我有一个基本的Node.js应用程序,我试图使用Express框架启动。我有一个views文件夹,其中有一个index.html文件。但是我在加载网页时收到以下错误:
Error: Cannot find module 'html'
下面是我的代码。
var express = require('express');
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.html');
});
app.listen(8080, '127.0.0.1')
我错过了什么?
It is very sad that it is about 2020 still express hasn't added a way to render an HTML page without using sendFile method of the response object. Using sendFile is not a problem but passing argument to it in the form of path.join(__dirname, 'relative/path/to/file') doesn't feel right. Why should a user join __dirname to the file path? It should be done by default. Why can't the root of the server be by defalut the project directory? Also, installing a templating dependency just to render a static HTML file is again not correct. I don't know the correct way to tackle the issue, but if I had to serve a static HTML, then I would do something like:
const PORT = 8154;
const express = require('express');
const app = express();
app.use(express.static('views'));
app.listen(PORT, () => {
console.log(`Server is listening at port http://localhost:${PORT}`);
});
The above example assumes that the project structure has a views directory and the static HTML files are inside it. For example, let's say, the views directory has two HTML files named index.html and about.html, then to access them, we can visit: localhost:8153/index.html or just localhost:8153/ to load the index.html page and localhost:8153/about.html to load the about.html. We can use a similar approach to serve a react/angular app by storing the artifacts in the views directory or just using the default dist/<project-name> directory and configure it in the server js as follows:
app.use(express.static('dist/<project-name>'));
来自Express.js指南:视图渲染
视图文件名采用Express形式。ENGINE,其中ENGINE是所需模块的名称。例如视图布局。Ejs会告诉视图系统require(' Ejs '),被加载的模块必须导出方法exports。render(str, options)来遵守Express,但是app.register()可以用来将引擎映射到文件扩展名,因此,例如foo.html可以由jade渲染。
所以你要么创建自己的简单渲染器,要么使用jade:
app.register('.html', require('jade'));
更多关于app.register的信息。
注意,在Express 3中,这个方法被重命名为app.engine
这里是一个完整的文件演示的express服务器!
https://gist.github.com/xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906
希望对你有所帮助!
// simple express server for HTML pages!
// ES6 style
const express = require('express');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const app = express();
let cache = [];// Array is OK!
cache[0] = fs.readFileSync( __dirname + '/index.html');
cache[1] = fs.readFileSync( __dirname + '/views/testview.html');
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.send( cache[0] );
});
app.get('/test', (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.send( cache[1] );
});
app.listen(port, () => {
console.log(`
Server is running at http://${hostname}:${port}/
Server hostname ${hostname} is listening on port ${port}!
`);
});
我试图用一个RESTful API创建一个angular应用,并多次登陆这个页面,尽管它并没有什么帮助。以下是我发现有效的方法:
app.configure(function() {
app.use(express.static(__dirname + '/public')); // set the static files location
app.use(express.logger('dev')); // log every request to the console
app.use(express.bodyParser()); // pull information from html in POST
app.use(express.methodOverride()); // simulate DELETE and PUT
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
});
然后在api的回调中,路由看起来像:res.jsonp(users);
您的客户端框架可以处理路由。Express是为API服务的。
我的回家路线是这样的:
app.get('/*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
这些答案很多都已经过时了。
使用快捷3.0.0和3.1.0,以下工作:
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
请参阅下面的注释,了解表达式3.4+的替代语法和注意事项:
app.set('view engine', 'ejs');
然后你可以这样做:
app.get('/about', function (req, res)
{
res.render('about.html');
});
这里假设views子文件夹中有视图,并且已经安装了ejs节点模块。如果不是,在Node控制台中执行以下命令:
npm install ejs --save