31 lines
921 B
JavaScript
31 lines
921 B
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
|
|
|
|
// 创建本地服务器来从其接收数据
|
|
//req 请求 res 响应
|
|
const server = http.createServer((req, res) => {
|
|
console.log(req)
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });// 'image/png' 返回的格式
|
|
// res.end(JSON.stringify({
|
|
// data: 'Hello World!'
|
|
// }));
|
|
//fs.readFileSync 同步 fs.readSync 异步 大文件用异步 小文件可以用同步
|
|
//且读到的是 Buffer
|
|
let data= fs.readFileSync("./index.html")
|
|
console.log(data)
|
|
console.log(data.toString())
|
|
res.end(data.toString())
|
|
// res.end(给出的数据) 给出的数据
|
|
});
|
|
//后端运行的端口
|
|
server.listen(8000);
|
|
|
|
const express =require("express")
|
|
const app =express();
|
|
const port =3000
|
|
app.get('/', (req,res)=>{
|
|
res.render('index',function(err,html){
|
|
res.send(html)
|
|
})
|
|
}) |