在 NodeJS REST API 中获取 404



我正在学习nodejs,当我在服务器中拥有所有功能时,我创建了很少的API.js它工作正常。 现在,当我在dbConfig.js,server.js和ProductController.js中分离我的代码时。我开始收到 404,但我的数据库连接工作正常。
服务器.js:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const conn = require('./connection/dbConfig');
const productRouter = require('./controller/ProductController');
// parse application/json
app.use(bodyParser.json());
//Server listening
app.listen(3000,() =>{
console.log('Server started on port 3000...');
});

还有我的产品控制器.js:

'user strict';
const conn = require('../connection/dbConfig');
const express = require('express');
const app = express();

//show all products
app.get('/getAllProducts',(req, res) => {
let sql = "SELECT * FROM product";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});

//show single product
app.get('/getProductById/:id',(req, res) => {
let sql = "SELECT * FROM product WHERE product_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});

//add new product
app.post('/addProduct',(req, res) => {
let data = {product_name: req.body.product_name, product_price: req.body.product_price};
let sql = "INSERT INTO product SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});

您的应用程序缺少的是路由的使用。基本上,您的应用程序当前不知道这些路由是在ProductController.js上定义的,因为您没有告诉它知道。

下面是一个快速修复程序,以及有关如何设置路由的快速示例:

您的主文件:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const conn = require('./connection/dbConfig');
const productRouter = require('./controller/ProductController');
// parse application/json
app.use(bodyParser.json());
// NEW LINE: You should tell the application to use the productRouter.
// The first parameter tells the base location of the route and the second tells where it's located on the project
app.use('/products', productRouter);
//Server listening
app.listen(3000,() =>{
console.log('Server started on port 3000...');
});

您的ProductController.js文件:

'use strict';
const conn = require('../connection/dbConfig');
const {Router} = require('express');
const route = new Router();

//show all products
route.get('/get',(req, res) => {
let sql = "SELECT * FROM product";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});

//show single product
route.get('/get/:id',(req, res) => {
let sql = "SELECT * FROM product WHERE product_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});

//add new product
route.post('/add',(req, res) => {
let data = {product_name: req.body.product_name, product_price: req.body.product_price};
let sql = "INSERT INTO product SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
module.exports = route;

您现在可以在路径/products/get/products/get/:id/products/add等访问这些位置。

PS:你在'use strict'行上加了一个小错误,应该是'use strict'而不是'user strict'

最新更新