javascript - 尝试从节点服务器检索 json 对象时获取“Cannot GET /”
问题描述
我正在编写一个 RESTful API。IT 使用 express.js 框架在 node.js 上运行,mongodb 使用 mongoose 作为对象建模工具和 body-parser 来传递 http. 每次我启动服务器并导航到指定的 IP 地址时,都会收到“CANNOT GET/”错误。我怎么能解决这个问题?一些建议将不胜感激。
我已经厌倦了使用不同的端口号,但问题仍然存在。这是我的 server.js 代码的副本:
var express = require('express'),
app = express(),
IP = process.env.IP,
port = process.env.PORT || 8080 ,
mongoose = require('mongoose'),
tasks = require('./api/models/todosModel'),
bodyParser = require('body-parser');
//handiling of promise
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Todosdb',{ useNewUrlParser: true });
app.use(bodyParser.urlencoded({extended:true})); // telling the sever instance to use body parser
app.use(bodyParser.json());
var Routes = require('./api/routes/todoRoutes');
//passing the server instance to the routes
Routes(app);
app.listen(port,IP);
console.log("The TODO API server is running on IP: " + IP + " and port: " + port);
todoRoute 代码:
'use strict';
module.exports = function(app){
var todofunctions = require('../controllers/todoController');
// todo routes
app.route('/tasks') //task [GET (all the tasks),POST]
.get(todofunctions.listTasks)
.post(todofunctions.createTask);
app.route('/tasks/:taskId') //a task [GET(single task),PUT,DELETE]
.put(todofunctions.updatetask)
.get(todofunctions.readTask)
.delete(todofunctions.deleteTask);
};
解决方案
这可能是因为您没有为/
.
尝试/tasks
在浏览器中转到,然后您会得到一些响应。
推荐阅读
- java - 每两个破折号的正则表达式并根据匹配更改字符串
- javascript - 如何访问作为函数参数的数组的长度?
- command - 是否有命令可以在 Stata 中获取最大的 5 个值和最小的 5 个值?
- ada - 如何获得整数的 10 对数?
- typescript - Apollo Server 如何提供其模式?
- sql - 在 BigQuery 中对 STRUCT 的 ARRAY 进行条件搜索
- javascript - Vuex 从状态加载现有表单数据以进行编辑
- rust - 使用 PEST 解析器的 PEG 语法的歧义
- reactjs - 状态钩子中的计数器不递增
- javascript - 选择选项时反应更新多个组件