將靜態頁面送進response
阿新 • • 發佈:2018-04-09
nodejs express 靜態頁面在response裏寫write或是end夾雜html代碼實在太low了,除了首頁反應快帶來問題一堆,將寫好的頁面pipe進去就容易維護多了。
Nodejs代碼:
‘use strict‘; });
app.listen(8080,"127.0.0.1"); 用戶名:<input type="text" name="name" value=""><br/>
密碼 :<input type="password" name="pswd" value=""><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
Nodejs代碼:
‘use strict‘;
var express=require(‘express‘);
var http=require(‘http‘);
var fs=require(‘fs‘);
var app=express();
app.get(‘/index.html‘,function(req,rsp){
rsp.writeHead(200,{‘Content-Type‘:‘text/html‘});
fs.createReadStream(‘index.html‘).pipe(rsp);
app.listen(8080,"127.0.0.1");
Html代碼:
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title> Add emp </title>
</head>
<body>
<form id="form1" action="index.html" method="post">
密碼 :<input type="password" name="pswd" value=""><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
頁面效果:
將靜態頁面送進response