我正在尝试设置一个nodejs服务器,并且无法通过邮递员从中获取响应
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require('mongoose');
//configure app for body parser
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json);
//setup server port
var port = process.env.PORT || 2345;
//connect to DB
mongoose.connect('mongodb://localhost:27017/helloapi', {
useMongoClient: true
});
//API routes
var router = express.Router();
//test routes
router.get("/", function (req, res) {
res.json({message: "welcome to our api"});
});
//routs will all be with prefix /api
app.use('/api', router);
//fire up server
app.listen(port);
//print to console when server fireup
console.log("Server listening on port: " + port);
即时启动mongod及其工作正常,当我启动文件时,我甚至看到了这一点:
2017-12-12T13:34:21.828+0200 I NETWORK [conn11] received client metadata from 127.0.0.1:53090 conn: { driver: { name: "nodejs", version: "2.2.33" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.16299" }, platform: "Node.js v9.2.1, LE, mongodb-core: 2.1.17" }
但是当邮寄员发送http://localhost:2345/api/时 我得不到任何回应
我在这里做错了什么? 感谢
答案 0 :(得分:0)
我发现了我使用的错误:
app.use(bodyParser.json);
然后我将其修复为:
app.use(bodyParser.json());
现在一切正常