使用極簡NodeJs代碼實現一個http靜態服務器 , 且能夠訪問並引用當前目錄下的HTML/CSS/JS等靜態資源

代碼 :javascript

const http = require('http');
const express = require('express');
const app = express();
const port = 8088;
app.use("/", express.static(__dirname + '/'));

// 建立服務端
http.createServer(app).listen(port, function () {
    console.log(`服務器啓動成功,能夠經過 http://localhost:${port}/ 來訪問`)
});

使用方法 :html

一、把該數據內容複製到一個JS文件中,如「app.js」;
二、把「app.js」文件放到須要做爲服務器的目錄下,文件發到「public」目錄下;
三、運行命令「node app.js」,若沒有安裝express模塊,運行命令「npm install express」進行安裝;
四、打開瀏覽器,訪問服務器:http://localhost:8088/index.html 便可java

使用極簡NodeJs代碼實現一個http靜態服務器node


訪問結果 :express

image.png

image.png

相關文章
相關標籤/搜索