繁体   English   中英

在nodejs中获得404 rest api

[英]getting 404 in nodejs rest api

我正在学习 nodejs,当我在 server.js 中拥有所有功能时,我创建了一些 API 它工作正常。 现在,当我在 dbConfig.js、server.js 和 ProductController.js 中分离我的代码时。 我开始收到 404,但我的数据库连接工作正常。
server.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...');
});

还有我的ProductController.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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM