邮递员返回404为node.js端点
我正在尝试使用node.js创build一个简单的rest端点。 我正在按照教程https://medium.freecodecamp.com/building-a-simple-node-js-api-in-under-30-minutes-a07ea9e390d2
文件夹结构是显着的/ app / routes /和routes文件夹包含index.js和note_routes.js文件
我能够运行命令npm run dev
,显示的输出是:
> notable@1.0.0 dev /Users/tejanandamuri/Desktop/notable > nodemon server.js [nodemon] 1.11.0 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node server.js` We are live on 8000
在这之后。 在邮差中,当我尝试调用http:// localhost:8000 / notes时 ,响应正文返回404错误无法POST /注释
这是我的文件:
server.js:
const express = require('express'); const MongoClient = require('mongodb').MongoClient; const bodyParser = require('body-parser'); const app = express(); const port = 8000; require('./app/routes')(app, {}); app.listen(port, () => { console.log('We are live on ' + port); });
index.js:
// routes/index.js const noteRoutes = require('./note_routes'); module.exports = function(app, db) { noteRoutes(app, db); // Other route groups could go here, in the future };
note_routes.js
module.exports = function(app, db) { app.post('/notes', (req, res) => { // You'll create your note here. res.send('Hello') }); };
的package.json:
{ "name": "notable", "version": "1.0.0", "description": "my first rest api", "main": "server.js", "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.3", "mongodb": "^2.2.28" }, "devDependencies": { "nodemon": "^1.11.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js", "dev": "nodemon server.js" }, "author": "venkata", "license": "ISC" }
将server.js中的第6行更改为require('./routes/note_routes')(app, {});
这假设你的文件树看起来像这样:
. +--/node_modules // this contains a ton of sub-folders +--/routes + +--index.js + +--note_routes.js +--package.json +--server.js