微博例子中"ReferenceError: crypto is not defined"
发布于 1年前 作者 aiwenming 2157 次浏览

app.js中已经添加了:var crypto = require(‘crypto’);

但是点击注册的时候显示错误如下:

POST /reg 500 42ms DEBUG: crashing child DEBUG: Starting child process with ‘node app.js’ Express server listening on port 3000 ReferenceError: crypto is not defined at exports.doReg (C:\Users\lin\blogA\routes\index.js:23:11) at callbacks (C:\Users\lin\blogA\node_modules\express\lib\router\index.j s:164:37) at param (C:\Users\lin\blogA\node_modules\express\lib\router\index.js:13 8:11) at pass (C:\Users\lin\blogA\node_modules\express\lib\router\index.js:145 :5) at Router._dispatch (C:\Users\lin\blogA\node_modules\express\lib\router
index.js:173:5) at Object.router (C:\Users\lin\blogA\node_modules\express\lib\router\ind ex.js:33:10) at next (C:\Users\lin\blogA\node_modules\express\node_modules\connect\li b\proto.js:190:15) at next (C:\Users\lin\blogA\node_modules\express\node_modules\connect\li b\middleware\session.js:312:9) at C:\Users\lin\blogA\node_modules\express\node_modules\connect\lib\midd leware\session.js:336:9 at C:\Users\lin\blogA\node_modules\connect-mongo\lib\connect-mongo.js:19 4:17 POST /reg 500 39ms

index.js代码如下:

exports.doReg = function(req, res) { //检验用户两次输入的口令是否一致 if(req.body[‘password-repeat’] != req.body[‘password’]) { req.flash('error’, ‘两次输入的口令不一致’); return res.redirect(‘/reg’); } //生成口令的散列值 var md5 = crypto.createHash(‘sha1’); var password = md5.update(req.body.password).digest(‘base64’); var newUser = new User({ name: req.body.username, password: password, }); //检查用户名是否已经存在 User.get(newUser.name, function(err, user) { if(user) err = 'Username already exists.’; if(err) { req.flash('error’, err); return res.redirect(‘/reg’); } //如果不存在则新增用户 newUser.save(function(err) { if(err) { req.flash('error’, err); return res.redirect(‘/reg’); } req.session.user = newUser; req.flash('success’, ‘注册成功’); res.redirect(‘/’); }); });

};

请问是什么原因,先谢谢了。

5 回复

检查是不是 crypto 拼写错误

不是写在app.js里的 是在index里边的吧

ReferenceError: crypto is not defined at exports.doReg (C:\Users\lin\blogA\routes\index.js:23:11)

这不是说明了在index.js 里没有找到crypto

我碰到的错误是ReferenceError: app is not defined,请问有什么解决方法

回到顶部