PJCHENder 未整理筆記

[Node] 傳送靜態網頁 send html

2018-09-03

[Node] 傳送靜態網頁 send html

@(NodeJS)[node, express]

Node.js

1
2
3
4
5
6
7
8
9
10
11
12
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
const html = fs.readFileSync('index.html', 'utf8');
res.write(html);
res.end();
});

server.listen(8000);

Express

1
2
3
4
let indexPage = path.join(__dirname + '/public/index.html');
app.get('/', (req, res) => {
res.sendFile(indexPage);
})

掃描二維條碼,分享此文章