我已经实现了包括用户注册/登录在内的小型应用程序,使用 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");
});
})
最佳答案
只是复制this comment作为答案,因为它对 OP 和我都有效:
he problem comes from the module negotiator that is used by one of your modules
npm uninstall negotiator
npm install negotiator
关于javascript - 错误 : Cannot find module './lib/mediaType' "(after successful login),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48372085/