JavaScript學習筆記(四) ─ 用Node.js來建立伺服器吧!

將以下程式碼存成server.js檔案

var http = require("http"); 
http.createServer(function(req,res){
res.writeHeader(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(8080);
console.log('Server started!');

使用Command Line :

node server.js 

此時,CMD會顯上Server started!,表示伺服器正常運作

接著,我們用瀏覽器打開http://localhost:8080/ 就能發現

網站出現Hello World的字樣

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store