简体   繁体   English

在Node.js中建立连接时出错

[英]Error while making a connection in Node.js

I get the following message whenever I run the code 每当我运行代码时,我都会收到以下消息

throw new Error('Most middleware (like ' + name + ') is no longer bundled with express and must be installed separately ^ Error: Most middleware(like BodyParser) is no longer bundled with Express and must be installed separately. 抛出新的错误(“大多数中间件(例如'+名称+')不再与express捆绑在一起,必须单独安装^错误:大多数中间件(例如BodyParser)不再与Express捆绑在一起,必须单独安装。

My code is as following: 我的代码如下:

var express = require("express"),
restful = require("node-restful");
mongoose = restful.mongoose;

var app = express();
app.use(express.bodyParser());
app.use(express.methodOverride());

mongoose.connect("mongodb://localhost/restful")

var ProductSchema = mongoose.Schema({
    name:String,
    sku:String,
    price:Number
});
var Products = restful.model("products", ProductSchema);
Products.methods(["get", "put", "post", "delete"]);
Products.register(app, "/api/products");

app.listen(3000);
Console.log("Server is running at port 3000")

I've installed npm body parser and npm method override separately but it still gives me the error message. 我已经分别安装了npm body解析器npm方法重写,但是它仍然给我错误消息。

It seems to me that you do not require body-parser. 在我看来,您不需要身体分析器。

You should have something like var bodyParser = require('body-parser'); 您应该有类似var bodyParser = require('body-parser'); and then use it like app.use(bodyParser.json()); 然后像app.use(bodyParser.json());一样使用它app.use(bodyParser.json()); if you want the json parser for example. 例如,如果您想要json解析器。

Since it is no longer in express, you cannot require it and use it through express 由于它已不再是快速快递,因此您不能要求它并通过快速快递使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM