我是node.js的完全新手,正在udemy.com上完成一个过时且半破坏的课程。
在之前的课程中,我已成功通过
等路线获取内容app.get('/vegetables',function(req,res) {
res.render('vegetables',{vegetables:vegetables});
})
在我目前的课程中我看到了
Error: EPERM: operation not permitted, open 'C:\members' at Error (native)
我无法理解为什么。应用程序按预期启动,不抛出任何警告或错误,__ dirname包含我期望的值,但每条路径都被解释为在本地根C :.
我做错了什么?
var express = require('express'),
path = require('path'),
app = express(),
bodyParser = require('body-parser'),
logger = require('express-logger'),
port = 9000,
MongoClient = require('mongodb').MongoClient;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(logger);
app.use(express.static(__dirname + '/public')); //establishes static file server on public dir
app.set('view engine', 'ejs');
MongoClient.connect('mongodb://127.0.0.1:27017/myExample',function(err,db) {
if(err) throw err;
var collection = db.collection('members');
var index = function(req,res) {
/*render the index view, and pass it members*/
collection.find().toArray(function(err,members) {
res.render('index',{members:members});
});
};
var addMember = function(req,res) {
collection.insert(req.body,function(err,docs) {
console.log(docs);
res.redirect('/members')
});
};
app.get('/members',index); //list all members
app.post('/members',addMember); //add a new member
app.listen(port);
console.log('server on %s',port);
console.log(__dirname );
});
process.on('uncaughtException',function(e) {
throw e;
});
答案 0 :(得分:1)
Logger需要一条路径:
app.use(logger({path: "path/filename.txt"}));
有没有更好的方法让我发现这个?
您可以确定在导致错误的行之后没有行。 console.log将起作用或记录如下行号的函数:
function logLineNumber() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
console.log('Ran line: ',stack[0].getLineNumber());
}
在表达中使用该功能:
app.use(function (req, res, next) {
logLineNumber();
next();
});
然后,您可以策略性地将此代码段放在代码中作为完整性检查,以确定错误是在控制台中记录的最后一个行号之后:
var express = require('express'),
app = express(),
path = require('path'),
bodyParser = require('body-parser'),
logger = require('express-logger'),
port = 9000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(function (req, res, next) {
logLineNumber();
next();
});
app.use(logger);
app.use(function (req, res, next) {
logLineNumber();
next();
});
app.use(function (req, res, next) {
logLineNumber();
next();
});
app.use(function (req, res, next) {
logLineNumber();
next();
});
app.use(express.static(__dirname + '/public')); //establishes static file server on public dir
app.set('view engine', 'ejs');
var index = function (req,res) {
// render the index view, and pass it members
console.log('ran index!');
res.render('index');
};
var redir = function (req,res) {
// render the index view, and pass it members
res.redirect('/');
};
app.use(function (req, res, next) {
logLineNumber();
next();
});
app.get('/',index); //list all members
app.get('/redir',redir); //add a new member
app.listen(port);
console.log('server on %s',port);
console.log(__dirname );
process.on('uncaughtException',function(e) {
throw e;
});
function logLineNumber() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
console.log('Ran line: ',stack[0].getLineNumber());
}
当我运行上面的命令并转到localhost:9000时,我得到控制台输出:
Ran line: 13
/Users/phillipsc/code/node/trash/stackoverflow/redirectRoot/index.js:71
throw e;
^
Error: EISDIR: illegal operation on a directory, open '/'
at Error (native)
直到第13行都没问题,因为它记录了'Ran line:13'。错误必须在第13行之后和第20行下一次调用logLineNumber之前发生,这会将其缩小到app.use(logger)。
修复记录器后,控制台将输出:
Ran line: 13
Ran line: 21
Ran line: 26
Ran line: 31
Ran line: 53
ran index!
此外,下面的代码可能会有所帮助,尽管它对具体的EISDIR错误没有帮助。
获得更大的堆栈跟踪:
Error.stackTraceLimit = Infinity;
在错误处理程序中打印堆栈:
console.error(e.stack);