Cannot find module 'express'
程序员文章站
2022-05-31 23:42:55
...
在学习《Nodejs开发指南》,其中在讲用express框架的时候,有个例子:
var express = require('express');
var app = express.createServer();
app.use(express.bodyParser());
app.all('/', function(req, res) {
res.send(req.body.title + req.body.text);
});
app.listen(3000);
运行报错,Error: Cannot find module 'express'
用命令install express -g安装后,再次运行,说没有bodyparser函数,查找资料发现,
新版的express中已经不包含bodyparser了,那就需要大家单独安装bodyparser,安装命令是npm install body-parser,然后在app.js中加载body-parser模块var bodyParser = require('body-parser'),把app.use(express.bodyParser())替换成app.use(bodyParser.urlencoded({ extended: false })),这样调试就没问题了。
调整后的代码为:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.all('/', function (req, res) {
res.send(req.body.title + req.body.text);
});
app.listen(3000);
运行就正常了。
上一篇: 如何发布一个npm包
推荐阅读
-
webpack-dev-server config.js Cannot find module
-
PHP动态编译出现Cannot find autoconf的解决方法
-
CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6
-
Linux安装Apache报错:Cannot find a valid baseurl for repo: base/7/x86_64解决方案
-
centos7安装docker后运行出现Cannot find a valid baseurl for repo: base/7/x86_64 错误,怎么解决?
-
Google App Engine (Java + String + Velocity)数据访问调试,出现错误 Cannot find class [javax
-
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
-
cannot find supercluster & native module cannot be null & Xcode build timeout
-
cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/sprin
-
刚安装的Linux Centos7使用yum安装firefox时提示:cannot find a valid baseurl for repo