2020年10月29日 星期四

重新踏入網頁開發 (3) - Route

Route ( URL )

    通過不同的 URL 去區別不同請求
  • Server.js
        import http from 'http'
        import url from 'url'
    
        function onRequest(request, response) {
            // 會紀錄 request url
            var path_name = url.parse(request.url).pathname;
            console.log("Request for " + path_name + " received");
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write("Hello World.\n");
            response.end();
        }
    
        http.createServer(onRequest).listen(8888);
        console.log("Server has started...");
  • 用 curl 去 request
        $ curl http://localhost:8888/
        Hello World.
        $ curl http://localhost:8888/students/1
        Hello World.
  • 伺服器端顯示
        $ node Server.js 
        Server has started...
        Request for / received
        Request for /students/1 received
上一篇 :
下一篇 :
參考資料 :

0 意見:

張貼留言

Popular Posts