首先,改变你的结构:
- 项目
- 资产/图像/
- assets/css/application.css
- assets/js/application.js
- 资产/字体
- node_modules
- views/index.html
- server.js
- package.json
首先需要一些包:
var express = require('express'),
app = express(),
path = require('path');
它们在终端窗口中运行:
npm install express
他们设置了配置:
app.set('views', path.join(__dirname, 'views')); // This is to serve static files like html in the views folder
app.set('view engine', html); // your engine, you can use html, jade, ejs, vash, etc
app.set('port', process.env.PORT || 80); // set up the port to be production or 80.
app.set('env', process.env.NODE_ENV || 'development');
app.use(express.static(path.join(__dirname, 'assets'))); // // This is to serve static files like .css and .js, images, fonts in the assets folder
他们创建你的路线:
app.get('/', function(req, res) {
res.send('Hello word');
});
app.get('/something', function(req, res) {
res.send('Hei, this is something!!!');
});
如果你想渲染索引,在views文件夹里面:
app.get('/index', function(req, res) {
res.render('index');
});
最后:
app.listen(app.get('port'), function(req, res) {
console.log('Server listening at ' + app.get('port')');
});
他们访问 localhost:80/index