var express = require('express');
var app = express();
var port = 8000;
app.use(express.static(__dirname + '/public'));/* 將預設路徑設在public*/
app.listen(port,() => console.log(`listening on ${port}`));
var express = require('express');
var app = express();
var port = 8000;
var router = require('./routes/index.js')(app);
app.use(express.static(__dirname + '/public'));/* 將預設路徑設在public*/
app.listen(port,() => console.log(`listening on ${port}`));
這時我們試著把
var router = require('./routes/index.js')(app);
改為
var router = require('./routes')(app);
發現還是可以,原因是require如果指定為資料夾,他會預設去找下面的index檔案
express 是一個架構在http上的框架
模板引擎
用於讓頁面與data分離的方式,在這裡我們要展示server side render data所以使用EJS當我們的範例
var express = require('express');
var app = express();
var port = 8000;
app.set('view engine', 'ejs');
var router = require('./routes')(app);
app.use(express.static(__dirname + '/public'));/* 將預設路徑設在public*/
app.listen(port,() => console.log(`listening on ${port}`));
routes裡面的index.js改為
module.exports = function (app) {
app.get('/', function (req, res) {
res.render('test1', { title: 'Hello', message: 'Hello there!'});
});
};