我是表达新手,我想探索 Express 的工作原理。
我正在使用最新版本的express 4 (4.8.1)
我不想使用任何模板引擎。我想手动提供我的 HTML 内容。
我怎么做?我在这里搜索了几个问题是 StackOverflow 并找到了解决方案。我应该做的就是注释掉以下几行。
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
但是当我这样做时,当我从浏览器访问我的服务器时会出现以下错误。
Error: No default engine was specified and no extension was provided.
at new View (/Users/adi/Desktop/srserver/node_modules/express/lib/view.js:41:42)
at Function.app.render (/Users/adi/Desktop/srserver/node_modules/express/lib/application.js:499:12)
at ServerResponse.res.render (/Users/adi/Desktop/srserver/node_modules/express/lib/response.js:955:7)
at module.exports (/Users/adi/Desktop/srserver/app.js:50:9)
at Layer.handle_error (/Users/adi/Desktop/srserver/node_modules/express/lib/router/layer.js:52:5)
at trim_prefix (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:261:13)
at /Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:230:9
at Function.proto.process_params (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:305:12)
at /Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:221:12
at Function.match_layer (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:288:3)
这是整个代码。(app.js)
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// 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: {}
});
});
module.exports = app;
app.listen(3000,function(){
console.log("Listening on #3000");
});
app.get("/",function(req,res){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Simple Simple Fun')
response.end();
});
另外,我jade
从package.json