我试图链接2服务器文件执行一些操作与URL,通过使用Node.js + express + Axios
链接结构为
FE Client [UI (VUE) -> PostService.js] -> BE Server [index.js -> posts_users.js -> DB]
我现在的处境是……
注册新用户工作完美(FE UI (value)) ->PostService.js (insert_Users_Post)→posts_users.js (router.post ('/'))
其他两个(修改和删除)得到以下错误
main.js吗?attr= gu7jfeermgn47forfib547yft_vvvswaynqc5thl4b1v8b4bwucnhxwmvsyhng6uowufyn_zukg_samebazuq:1078 PATCH http://localhost:8080/User_Management/api/posts_users/6049b4cbc3c1a94668af9e64 404 (Not Found)
vue.esm.js?a026:1897错误:请求失败,状态码404at createError (createError.js?2d83:16)在安顿(安顿.js?467f:17)在XMLHttpRequest。handleLoad (xhr.js ? b50d: 62)
奇怪的是…
- 在PostService.js代码中,我检查了它是否收到'_id'值[ex] _id = 6049a5238651e35bc0cf449c]
- 和其他UI选项也引用了类似的代码结构,但工作良好....
有什么建议....修复错误…?
这是我的PostService.js代码
import axios from "axios";
const url_users = "api/posts_users/";
class PostService {
//Create users post
static insert_Users_Post(createdAt, userID, firstName, lastName, max_temp, phoneNum, emailAddr, department, deviceName, role, status) {
return axios.post(url_users, {
createdAt, userID, firstName, lastName, max_temp, phoneNum, emailAddr, department, deviceName, role, status
});
}
//update users post
static update_Users_Post(_id, createdAt, userID, firstName, lastName, max_temp, phoneNum, emailAddr, department, deviceName, role, status) {
return axios.patch(url_users+ `${_id}`, {
createdAt, userID, firstName, lastName, max_temp, phoneNum, emailAddr, department, deviceName, role, status
});
}
//delete users post
static delete_Users_Post(_id) {
return axios.delete(`${url_users}${_id}`);
}
}
这是我的posts_users.js代码
const express = require('express');
const mongodb = require('mongodb');
const router = express.Router();
var config = require('../../../config');
// Add User Post
router.post('/', async(req, res) => {
const posts = await loadPostsCollection_Users();
await posts.insertOne({
userID: req.body.userID,
firstName: req.body.firstName,
lastName: req.body.lastName,
phoneNum: req.body.phoneNum,
emailAddr: req.body.emailAddr,
department: req.body.department,
deviceName: req.body.deviceName,
role: req.body.role,
createdAt : req.body.createdAt,
updatedAt : req.body.createdAt,
createdVia:"Web",
status: req.body.status,
active: "false",
max_temp: req.body.max_temp,
});
res.status(201).send();
});
// Modify User Post
router.patch('/:id', async(req, res) => {
const posts = await loadPostsCollection_Users();
await posts.updateOne(
{ "_id" : new mongodb.ObjectID(req.params.id) },
{$set: {
"userID": req.body.userID,
"firstName": req.body.firstName,
"lastName": req.body.lastName,
"phoneNum": req.body.phoneNum,
"emailAddr": req.body.emailAddr,
"department": req.body.department,
"deviceName": req.body.deviceName,
"role": req.body.role,
"updatedAt" : req.body.createdAt,
"createdVia":"Web",
"status": req.body.status,
"active": "false",
"max_temp": req.body.max_temp
}},
{ upsert: true }
);
res.status(201).send();
});
// Delete User Post
router.delete('/:id', async(req, res) => {
const posts = await loadPostsCollection_Users();
await posts.deleteOne({
_id: new mongodb.ObjectID(req.params.id)
});
res.status(200).send();
});
async function loadPostsCollection_Users() {
const client = await mongodb.MongoClient.connect(`mongodb://${config.DB_IPAddr}:${config.DB_PortNo}/tms`, {
useNewUrlParser: true
});
return client.db('tms').collection('integrated_users');
}
module.exports = router;
最后这是我的BE index.js代码
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
// Middleware
app.use(bodyParser.json());
app.use(cors());
const posts_users = require('./routes/api/posts_users');
app.use('/api/posts_users', posts_users);
// Handle production
if(process.env.NODE_ENV === 'production'){
//Static folder
app.use(express.static(__dirname + '/public/'));
//Handle SPA
app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html'));
}
const port = process.env.PORT || 5002;
app.listen(port, () => console.log(`Server started on port ${port}`));
请确保在Axios调用时正确添加base url。
const url_users ="api/posts_users/";;