我得到一个module.js:339 throw err;
我不确定哪条线或哪个地方需要修理它。如果我能指出正确的方向,我想我会解决它。我正在搜索错误的信息,我似乎无法弄清楚为什么以及如何以及如何解决错误。
var bodyParser = require('body-parser');
var express = require('express');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var User = mongoose.model('User', new Schema({
id: ObjectId,
firstName: String,
lastName: String,
email: { type: String, unique: true },
password: String,
}));
var app = express();
app.set('view engine', 'jade');
app.locals.pretty = true;
// conenct to mongo
mongoose.connect('mongodb://localhost/auth');
// middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(req, re6s) {
res.render('index.jade');
});
app.get('/register', function(req, res) {
res.render('register.jade');
});
app.post('/register', function(req, res) {
var user = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password
});
user.save(function(err) {
if (err) {
var err = "Something bad happened! Try again!";
if (err.code === 11000) {
error = "That email is already taken. Try another.";
}
res.render("register.jade", { error: error });
} else {
res.redirect("/dashboard");
}
});
});
app.get('/login', function(req, res) {
res.render('login.jade');
});
app.get('/dashboard', function(req, res) {
res.render('dashboard.jade');
});
app.get('/logout', function(req, res) {
res.redirect('/');
});
app.listen(3000);

完整错误消息:
>node app.js
module.js:339
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (C:\Users\mechart\Desktop\auth\app.js:2:15)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
答案 0 :(得分:1)
Can't find module 'Express'
表示快速模块未正确安装在可从项目访问的路径中。您需要使用NPM安装该模块。与您在此项目中使用的任何其他模块相同。它应该像更改到应用程序目录一样简单,然后在控制台中发出以下命令:
npm install express
您还有另一个应该修复的错误。
重复的变量定义导致错误处理无法正常工作
在此代码中,您有一个名为err
的参数,然后您还有var err
。这将使你的参数被本地声明的var
覆盖,你的错误处理将无效:
改变这个:
user.save(function(err) {
if (err) {
var err = "Something bad happened! Try again!";
if (err.code === 11000) {
error = "That email is already taken. Try another.";
}
res.render("register.jade", { error: error });
} else {
res.redirect("/dashboard");
}
});
到此:
user.save(function(err) {
if (err) {
// change this variable name to errMsg in each place you use it
var errMsg = "Something bad happened! Try again!";
if (err.code === 11000) {
errMsg = "That email is already taken. Try another.";
}
res.render("register.jade", { error: errMsg });
} else {
res.redirect("/dashboard");
}
});