почтальон, возвращающий 404 для конечной точки node.js

1

Я пытаюсь создать простую конечную точку отдыха, используя node.js. Я следую учебнику https://medium.freecodecamp.com/building-a-simple-node-js-api-in-under-30-minutes-a07ea9e390d2

структура папки примечательна/приложение/маршруты/, а папка маршрутов содержит файлы index.js и note_routes.js

Я могу запустить команду npm run dev, а отображаемый результат:

> [email protected] 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"
}
Теги:

1 ответ

1

Измените строку 6 в server.js, чтобы require('./routes/note_routes')(app, {});

Это предполагает, что ваше дерево файлов выглядит примерно так:

.
+--/node_modules // this contains a ton of sub-folders
+--/routes
+  +--index.js
+  +--note_routes.js
+--package.json
+--server.js

Изображение 174551

  • 0
    после того, как я изменил путь в require, выдается следующая ошибка: Ошибка: не удается найти модуль './note_routes'
  • 0
    Попробуйте изменить его на require('./routes/note_routes')(app, {}); не заметил, что вы упомянули структуру папок.
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню