Nodejs开发指南出现的问题汇总

Nodejs开发指南出现的问题汇总

问题一: 安装 ejs 模板的语法有问题,安装不成功,如下: express -t ejs microblog

需要改成: express -e ejs microblog

问题二: partial 方法已经不能用了,可以用include代替,如下: <ul><%- partial(‘listitem’, items) %></ul>

需要改成: <% items.forEach(function(listitem){ %> <% include listitem %> <% }) %>

问题三: helpers 和 dynamicHelpers 方法已经不能用了,如下: app.helpers({ inspect: function(obj) { return util.inspect(obj, true); } }); app.dynamicHelpers({ headers: function(req, res) { return req.headers; } }); app.get(’/helper’, function(req, res) { res.render(‘helper’, { title: ‘Helpers’ }); });

需要改成: var util = require(‘util’); app.locals({ inspect: function(obj){ return util.inspect(obj, true); } }); app.use(function(req, res, next){ res.locals.headers = req.headers; next(); }); app.get(’/helper’, function(req, res){ res.render(‘helper’,{ title: ‘Helpers’ }); });

还需要注意的是,上面这段代码需要放在 app.use(app.router); 前面。

问题四: express3.*已经不支持layout方法了,所以要改成include才能正常显示首页。 在 views 文件夹下新建,header.ejs 和 footer.ejs。 layout.ejs 中的内容,以 <%- body %> 为界限,上面的内容写入header.ejs ,下面的内容写入footer.ejs 然后在 index.js 中加入 <% include header.ejs %> 和 <% include footer.ejs %>,把表单内容,写在中间即可

问题五: 配置mongodb时,很多报错: app.js中的 var settings = require(’…/settings’); 应改成 var settings = require(’./settings’); app.js中的 app.use(express.bodyParser()); 应该去掉 app.js中的 var MongoStore = require(‘connect-mongo’); 应改成 var MongoStore = require(‘connect-mongo’)(express);

问题六: 出现 has no method ‘router’ 问题,解决办法如下: 保留原来的 app.use(app.router); 不要按作者的说法改成 app.use(express.router(routes)); 并且在 app.js 最末尾加上 routes(app); 而且还要删除掉 app.js 中的 app.get(’/’, routes.index); app.get(’/u/:user’, routes.user); app.post(’/post’, routes.post); app.get(’/reg’, routes.reg); app.post(’/reg’, routes.doReg); app.get(‘login’, routes.login); app.post(‘login’, routes.doLogin); app.get(’/logout’, routes.logout);

问题七: req.flash 方法不能用,解决办法如下: 运行>npm install connect-flash 安装组件 并在app.js中加入: var flash = require(‘connect-flash’); app.use(flash());

问题八: Error: Cannot use a writeConcern without a provided callback at Db.ensureIndex (D:\Work\code\nodejs\microblog\node_modules\mongodb\lib\mongodb\db.js:1395:11) 解决办法如下: \models\user.js 中的 collection.ensureIndex(‘name’, {unique: true}); 改成collection.ensureIndex(‘name’, {unique: true}, function(err, user){}); \models\post.js 中的 collection.ensureIndex(‘user’); 改成collection.ensureIndex(‘user’ ,function(err, post){});


7 回复

Nodejs开发指南出现的问题汇总

在使用 Node.js 开发时,可能会遇到一些常见的问题。以下是这些问题及其解决方案的汇总。

问题一:安装 ejs 模板的语法有问题

错误代码:

express -t ejs microblog

正确代码:

express -e microblog

解释: express 命令的 -t 参数已经被 -e 取代,用来指定模板引擎。

问题二:partial 方法已经不能用了

错误代码:

<ul><%- partial('listitem', items) %></ul>

正确代码:

<% items.forEach(function(listitem){ %>
<%- include listitem %>
<% }) %>

解释: partial 方法已经被废弃,推荐使用 include 方法来包含部分视图。

问题三:helpers 和 dynamicHelpers 方法已经不能用了

错误代码:

app.helpers({
    inspect: function(obj) {
        return util.inspect(obj, true);
    }
});
app.dynamicHelpers({
    headers: function(req, res) {
        return req.headers;
    }
});

正确代码:

var util = require('util');
app.locals.inspect = function(obj) {
    return util.inspect(obj, true);
};

app.use(function(req, res, next) {
    res.locals.headers = req.headers;
    next();
});

app.get('/helper', function(req, res) {
    res.render('helper', {
        title: 'Helpers'
    });
});

解释: app.helpersapp.dynamicHelpers 已经被废弃。现在应该使用 app.locals 来设置全局变量,并使用中间件来设置局部变量。

问题四:express3.*已经不支持layout方法了

错误代码:

<!-- layout.ejs -->
<body>
    <%- body %>
</body>

正确代码:

<!-- header.ejs -->
<header>
    <h1>Microblog</h1>
</header>

<!-- footer.ejs -->
<footer>
    <p>Copyright © 2023 Microblog</p>
</footer>

<!-- index.ejs -->
<% include header.ejs %>
<!-- 表单内容 -->
<% include footer.ejs %>

解释: 在 Express 3.x 中,布局(layout)功能已经被移除。你可以通过分别创建 header.ejsfooter.ejs 来实现类似的功能。

问题五:配置 MongoDB 时出现错误

错误代码:

var settings = require('../settings');
app.use(express.bodyParser());
var MongoStore = require('connect-mongo');

正确代码:

var settings = require('./settings');
var MongoStore = require('connect-mongo')(express);

解释:

  1. require 路径错误,应该使用相对路径 ./settings
  2. express.bodyParser() 已被废弃。
  3. MongoStore 的导入方式需要传入 express 对象。

问题六:has no method ‘router’ 错误

错误代码:

app.use(express.router(routes));

正确代码:

app.use(app.router);
routes(app);

// 删除以下代码
app.get('/', routes.index);
app.get('/u/:user', routes.user);
app.post('/post', routes.post);
app.get('/reg', routes.reg);
app.post('/reg', routes.doReg);
app.get('/login', routes.login);
app.post('/login', routes.doLogin);
app.get('/logout', routes.logout);

解释: 保留 app.use(app.router); 并在最后调用 routes(app)。同时删除所有手动定义的路由。

问题七:req.flash 方法不能用

解决办法:

npm install connect-flash

正确代码:

var flash = require('connect-flash');
app.use(flash());

解释: 使用 connect-flash 组件来实现 Flash 消息。

问题八:Error: Cannot use a writeConcern without a provided callback

错误代码:

collection.ensureIndex('name', {unique: true});

正确代码:

collection.ensureIndex('name', {unique: true}, function(err, user) {});

解释: 确保在执行数据库操作时提供回调函数来处理可能的错误。


唉 新版本改的东西好多 ,相当于刚刚尝试hello world ,结果就GG了…

毕竟两年前的书了

嗯,不过一些东西改了还可以作为参考。

在express4.x版本中,执行node app.js无效,需要执行npm start

app.locals({ inspect: function(obj){ return util.inspect(obj, true); } }); 少了一个等于

app.locals = ({ inspect: function(obj){ return util.inspect(obj, true); } });

Nodejs开发指南出现的问题汇总

问题一:

问题描述: 安装 ejs 模板时使用旧命令导致安装失败。

解决方案: 将命令 express -t ejs microblog 改为 express -e ejs microblog

问题二:

问题描述: 旧版本中使用的 partial 方法在新版中已弃用。

解决方案:<%- partial('listitem', items) %> 改为:

<% items.forEach(function(listitem){ %>
<%- include listitem %>
<% }) %>

问题三:

问题描述: 旧版 helpersdynamicHelpers 方法在新版中已被移除。

解决方案: 改为使用 app.locals 和中间件来设置全局变量。示例如下:

var util = require('util');

app.locals.inspect = function(obj){
    return util.inspect(obj, true);
};

app.use(function(req, res, next){
    res.locals.headers = req.headers;
    next();
});

app.get('/helper', function(req, res){
    res.render('helper',{
        title: 'Helpers'
    });
});

注意: 上述代码应放置在 app.use(app.router); 之前。

问题四:

问题描述: Express 3.x 版本中不支持 layout 方法,需要通过 include 实现布局功能。

解决方案:views 文件夹下创建 header.ejsfooter.ejs,将 layout.ejs 的内容拆分到这两个文件中。在 index.js 中使用:

<%- include header.ejs %>
<!-- 表单内容 -->
<%- include footer.ejs %>

问题五:

问题描述: 配置 MongoDB 连接时遇到错误。

解决方案:

  1. var settings = require('../settings'); 改为 var settings = require('./settings');
  2. 删除 app.use(express.bodyParser());
  3. var MongoStore = require('connect-mongo'); 改为 var MongoStore = require('connect-mongo')(express);

问题六:

问题描述: 出现 has no method 'router' 错误。

解决方案: 保留 app.use(app.router); 并在 app.js 末尾添加 routes(app);,同时删除以下路由定义:

app.get('/', routes.index);
app.get('/u/:user', routes.user);
app.post('/post', routes.post);
app.get('/reg', routes.reg);
app.post('/reg', routes.doReg);
app.get('/login', routes.login);
app.post('/login', routes.doLogin);
app.get('/logout', routes.logout);

问题七:

问题描述: req.flash 方法在新版中不可用。

解决方案: 安装 connect-flash 组件,并在 app.js 中添加:

var flash = require('connect-flash');
app.use(flash());

问题八:

问题描述: 在使用 ensureIndex 时遇到错误。

解决方案: 在模型文件中添加回调函数,如:

// 在 models/user.js 中
collection.ensureIndex('name', {unique: true}, function(err, user){});

// 在 models/post.js 中
collection.ensureIndex('user', function(err, post){});

以上是针对常见问题的解决方案和示例代码。希望这些信息能够帮助你顺利进行 Node.js 开发。

回到顶部