【问题标题】:What is wrong with my localhost:5000/users url in this node.js project?这个 node.js 项目中我的 localhost:5000/users url 有什么问题?
【发布时间】:2020-11-14 22:38:42
【问题描述】:

我正在使用 node.js 构建一个 REST CRUD,并检查我的 url 是否正常工作,当我访问 url“localhost:5000/users”时,我试图获取“Hello1”消息,但所有当我访问它时,我得到了不能 GET /users。我真的不知道我的代码有什么问题,因为我完全按照教程所说的进行操作。有人知道怎么回事吗?

users.js 文件:

import express from 'express';

const router = express.Router();


router.get('/' , (req,res) => {
    res.send("Hello1");
});

export default router;

//////////////////////////////// index.js 文件

import express from 'express';
import bodyParser from 'body-parser';
import usersRoutes from './routes/users.js';


const app = express();
const PORT = 5000;

app.use(bodyParser.json());


app.get('/', (req,res) => {
    res.send("hello");
})

app.get('/users', usersRoutes);

app.listen(PORT, () => console.log("Server running on localhost:"+PORT));

【问题讨论】:

    标签: javascript node.js express url


    【解决方案1】:

    代替:

    app.get('/users', usersRoutes);
    

    你应该使用:

    app.use('/users', usersRoutes);
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2015-08-27
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多