为什么对 node.js body-parser 的调用会失败,尽管我已经安装了它?
Why does the call to node.js body-parser fail despite the fact that I have installed it?
我开始学习 node.js 并尝试找出如何获取 POST 请求的内容。我正在尝试按照说明进行操作 in this post。
到目前为止,我已经成功安装了 node.js(在 Windows 7 上)和 express,并且能够让我的第一个脚本运行。但是,当我尝试使用 body-parser 时,我的问题就来了。我已经安装了它,它似乎在那里
这是node.js脚本的代码
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(express.json()); // to support JSON-encoded bodies
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('Vous êtes à l\'accueil');
});
app.get('/user/:usernum', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('You are on page USER with n° : ' + req.params.usernum);
});
//
app.post('/adonis', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
console.log(req.body.title);
// res.write(JSON.stringify(req));
res.end('Hopefully I stringified a POST');
});
// ... Tout le code de gestion des routes (app.get) se trouve au-dessus
app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
});
app.listen(8091);
然而当我 运行 它时,node.js 抛出一个错误说 "cannot find module body-parser"。我做错了什么?
根据@Kale 和其他人的建议,我尝试在本地安装 body-parser,但这似乎没有帮助,因为现在我的脚本给出了以下消息:
Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (d:\smartguide\nodejs\node_modules\express\lib\express.js:99:13)
at Object.<anonymous> (d:\smartguide\nodejs\oc1.js:5:16)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3`
我尝试在本地和全局安装 "json" - 安装似乎有效,但对文件错误没有影响。
正如 Kevin B 所述,您必须在本地body-parser
安装并将其保存到清单中:
npm install --save body-parser
我认为我做错了根本性的事情 - 我回到基础并重新开始,这次确保我有一个 package.json 文件。现在可以了。
代码如下:
var express = require('express');
var session = require('cookie-session');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();
var app = express();
// JSON testing
app.post('/json-test', jsonParser, function(req, res) {
if (!req.body) return res.sendStatus(400);
console.log(JSON.stringify(req.body));
console.log(req.body.title);
res.status(200).send(req.body.title);
})
// Can't get anything else
.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
})
.listen(8090);
这里是 package.json
{
"name": "todo1",
"version": "0.1.0",
"dependencies": {
"express": "~4.11.0",
"ejs": "~2.1.4",
"cookie-session": "~1.1.0",
"body-parser": "~1.10.1"
},
"author": "Martin",
"description": "Un gestionnaire de todolist ultra basique"
}
这个答案简单多了。转到基本目录并 link 到所需的全局模块。
npm link body-parser
无需到处安装模块。如果模块没有全局安装,上面的命令将全局安装模块,然后 link 在本地安装它。
我遇到了同样的错误,在安装 express 之后我遇到了类似
的错误
Cannot find module 'body-parser'
安装后错误是
Cannot find module 'merge-descriptors'
等等
Cannot find module 'finalhandler'
Cannot find module 'array-flatten'
这些模块都是 express 的依赖项。如果您在没有任何模块的情况下执行 "npm install" 或 "npm install -g",它将安装所有缺少的依赖项。
为了解决这个问题,我先卸载 express,然后安装它,然后立即执行 "npm install"。这修复了所有错误。
我开始学习 node.js 并尝试找出如何获取 POST 请求的内容。我正在尝试按照说明进行操作 in this post。
到目前为止,我已经成功安装了 node.js(在 Windows 7 上)和 express,并且能够让我的第一个脚本运行。但是,当我尝试使用 body-parser 时,我的问题就来了。我已经安装了它,它似乎在那里
这是node.js脚本的代码
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(express.json()); // to support JSON-encoded bodies
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('Vous êtes à l\'accueil');
});
app.get('/user/:usernum', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('You are on page USER with n° : ' + req.params.usernum);
});
//
app.post('/adonis', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
console.log(req.body.title);
// res.write(JSON.stringify(req));
res.end('Hopefully I stringified a POST');
});
// ... Tout le code de gestion des routes (app.get) se trouve au-dessus
app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
});
app.listen(8091);
然而当我 运行 它时,node.js 抛出一个错误说 "cannot find module body-parser"。我做错了什么?
根据@Kale 和其他人的建议,我尝试在本地安装 body-parser,但这似乎没有帮助,因为现在我的脚本给出了以下消息:
Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (d:\smartguide\nodejs\node_modules\express\lib\express.js:99:13)
at Object.<anonymous> (d:\smartguide\nodejs\oc1.js:5:16)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3`
我尝试在本地和全局安装 "json" - 安装似乎有效,但对文件错误没有影响。
正如 Kevin B 所述,您必须在本地body-parser
安装并将其保存到清单中:
npm install --save body-parser
我认为我做错了根本性的事情 - 我回到基础并重新开始,这次确保我有一个 package.json 文件。现在可以了。
代码如下:
var express = require('express');
var session = require('cookie-session');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();
var app = express();
// JSON testing
app.post('/json-test', jsonParser, function(req, res) {
if (!req.body) return res.sendStatus(400);
console.log(JSON.stringify(req.body));
console.log(req.body.title);
res.status(200).send(req.body.title);
})
// Can't get anything else
.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
})
.listen(8090);
这里是 package.json
{
"name": "todo1",
"version": "0.1.0",
"dependencies": {
"express": "~4.11.0",
"ejs": "~2.1.4",
"cookie-session": "~1.1.0",
"body-parser": "~1.10.1"
},
"author": "Martin",
"description": "Un gestionnaire de todolist ultra basique"
}
这个答案简单多了。转到基本目录并 link 到所需的全局模块。
npm link body-parser
无需到处安装模块。如果模块没有全局安装,上面的命令将全局安装模块,然后 link 在本地安装它。
我遇到了同样的错误,在安装 express 之后我遇到了类似
的错误Cannot find module 'body-parser'
安装后错误是
Cannot find module 'merge-descriptors'
等等
Cannot find module 'finalhandler'
Cannot find module 'array-flatten'
这些模块都是 express 的依赖项。如果您在没有任何模块的情况下执行 "npm install" 或 "npm install -g",它将安装所有缺少的依赖项。
为了解决这个问题,我先卸载 express,然后安装它,然后立即执行 "npm install"。这修复了所有错误。