我目前正在尝试使用Angular 2中的XMLHttpRequest协议将文件上传到服务器。至于数据从客户端传输到服务器我遇到了这个错误:
POST /upload_file 500 54.155 ms - 1398
Error: Failed to lookup view "error" in views directory "C:\Users\...\Documents\right-track\server"
at EventEmitter.render (C:\Users\...\Documents\right-track\server\node_modules\express\lib\application.js:579:17)
at ServerResponse.render (C:\Users\...\Documents\right-track\server\node_modules\express\lib\response.js:961:7)
at C:\Users\...\Documents\right-track\server\server.js:56:7
at Layer.handle_error (C:\Users\...\Documents\right-track\server\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\Users\e...\Documents\right-track\server\node_modules\express\lib\router\index.js:310:13)
at C:\Users\...\Documents\right-track\server\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\...\Documents\right-track\server\node_modules\express\lib\router\index.js:330:12)
at IncomingMessage.next (C:\Users\...\Documents\right-track\server\node_modules\express\lib\router\index.js:271:10)
at done (C:\Users\...\Documents\right-track\server\node_modules\express\lib\response.js:956:25)
at EventEmitter.render (C:\Users\...\Documents\right-track\server\node_modules\express\lib\application.js:581:14)
我认为我甚至不打算使用'错误'视图,因为它是一个单页应用程序。以下是我设置节点服务器的方法:
//Set view engine
app.set('views', path.join(__dirname, ''));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
app.use('/', routes);
app.use('/users', users);
app.use('/upload', upload);
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log('Server running on port '+ port+'...');
})
module.exports = app;
- Upload.js
// POST - Upload File
router.post('/upload_file',upload.array("upload", 12),function(req, res){
res.render('showcase');
//res.status(200).send(req.files);
console.log(file);
})
答案 0 :(得分:0)
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
据我所知,总是会在每个请求上抛出错误。然后下一个处理程序尝试使用'错误'视图哪个不存在。
查看中间件的工作原理。您可以找到一个好的现有模板或框架来设置带节点的Angular 2 SPA。