我已经实施了小型应用程序,包括用户注册/登录,使用 Passport 和 Express 进行路由和mongodb / node.js。
我没有使用任何可选的登录Facebook或Google等代码已经100%正常工作。几周后检查一些更改并成功登录后,由console.log和MongoDB窗口日志验证
我收到以下错误:
Error: Cannot find module './lib/mediaType'"
发生错误是为了响应app.post('/login)
中的res.redirect行。请参阅下面的相关代码。
当我注意这一行并且只是console.log时没有问题
完整的错误消息:
successful login authentication
module.js:540
throw err;
^
Error: Cannot find module './lib/mediaType'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at loadModule (D:\GitHub\js_exmpls\authentication\node_modules\negotiator\index.js:114:16)
at Negotiator.mediaTypes (D:\GitHub\js_exmpls\authentication\node_modules\negotiator\index.js:76:29)
at Accepts.type.Accepts.types (D:\GitHub\js_exmpls\authentication\node_modules\accepts\index.js:105:33)
at IncomingMessage.req.accepts (D:\GitHub\js_exmpls\authentication\node_modules\express\lib\request.js:134:23)
at ServerResponse.res.format (D:\GitHub\js_exmpls\authentication\node_modules\express\lib\response.js:627:11)
at ServerResponse.redirect (D:\GitHub\js_exmpls\authentication\node_modules\express\lib\response.js:888:8)
at D:\GitHub\js_exmpls\authentication\app.js:113:6
at D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\middleware\authenticate.js:249:29
at D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\http\request.js:51:48
at D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\sessionmanager.js:16:14
at pass (D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\authenticator.js:297:14)
at Authenticator.serializeUser (D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\authenticator.js:299:5)
at SessionManager.logIn (D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\sessionmanager.js:14:8)
at IncomingMessage.req.login.req.logIn (D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\http\request.js
:50:33)
at Strategy.strategy.success (D:\GitHub\js_exmpls\authentication\node_modules\passport\lib\middleware\authenticate.j
s:248:13)
at verified (D:\GitHub\js_exmpls\authentication\node_modules\passport-local\lib\strategy.js:83:10)
at PBKDF2.ondone (D:\GitHub\js_exmpls\authentication\node_modules\passport-local-mongoose\lib\authenticate.js:48:12))
我已经安装了媒体类型库并在我的app.js中添加了以下行:
"var mediaType = require('media-type');"
结果是错误消息没有变化。在app.post('/login)
内响应res.redirect行时发生错误。请参阅以下相关代码:
当我注意到这一行并且只是console.log时没有问题。
代码:
var http = require('http');
var mongoose = require('mongoose');
var mongodb=require('mongodb');
var express=require('express');
var csv = require('csv');
var bodyParser= require('body-parser');
var fs = require('fs');
var path=require('path');
var assert= require('assert');
var User= require("./models/user.js");
var passport= require('passport');
var mediaType = require('media-type');
var LocalStrategy= require('passport-local');
var passportLocalMongoose= require('passport-local-mongoose');
var mongoClient=require('mongoose');
var app = express();
var Schema = mongoose.Schema;
.
.
app.get('/login', function (req, res) {
res.render('login' );
})
.
.
app.post("/login",function(req,res,err){
if (err) {console.log(err); };//else not needed render is end
passport.authenticate("local")(req,res,function(){
console.log("successful login authentication");
res.redirect("./views/friends.ejs");
});
})
答案 0 :(得分:0)
只需复制this comment作为答案,因为它对OP和我都起作用:
问题来自您的模块之一使用的模块协商器
npm uninstall negotiator
npm install negotiator