全端入门Day24_後端程序撰写之多一点点的Node.js

昨天介绍了一些名词,今天继续提Node.js

Node.js一点入门

今天直接贴上程序码,再去做解释

var http = require('http');
var server = http.createServer(function(request,response){
        if(request.url == '/'){
            response.writeHead(200,{'Content-Type':'text/html'});
            response.write('<html><body>welcome</body></html>');
            response.end();
        }else if(request.url == '/shop'){
            response.writeHead(200,{'Content-Type':'text/html'});
            response.write('<html><body>shop</body></html>');
            response.end();
        }else if(request.url == '/game'){
            response.writeHead(200,{'Content-Type':'text/html'});
            response.write('<html><body>game</body></html>');
            response.end();
        }else
            response.end('error');
});
server.listen(12345);
console.log('Server running at http://127.0.0.1:12345/')

直接存档,然後一样用npm home.js,之後打开浏览器打127.0.0.1/12345会看到这个
https://ithelp.ithome.com.tw/upload/images/20210906/201401269hF9ZFPevn.jpg
这样就能说明程序码了,这次改了中间的部分,简单的说就是网址後面(127.0.0.1/12345)如果有shop就会到shop页面(127.0.0.1/12345/shop),如果是game就会到game页面(127.0.0.1/12345/game),如果都不是就会是error。
https://ithelp.ithome.com.tw/upload/images/20210906/2014012615EuzrFkGn.jpg
https://ithelp.ithome.com.tw/upload/images/20210906/20140126kmXQmQupYH.jpg
https://ithelp.ithome.com.tw/upload/images/20210906/20140126MmgizMQdWn.jpg
之後我们打开F12并点NetWork,会像是这样:
https://ithelp.ithome.com.tw/upload/images/20210906/20140126khMc3rOb2i.jpg
这里就能看的出状态码是200,代表是有成功的,那麽在继续往下看,点击shp[
https://ithelp.ithome.com.tw/upload/images/20210906/201401262GPNpLilj2.jpg
我们可以看到更仔细的内容:
Request Method
这个在昨天没有说明到,主要有GET、POST、PUT、DELETE还有其他的,我们要资料就是用GET,如果我们是要上传资料就是用POST,如果我们是PUT则是要覆盖资料,那麽DELETE就是删除资料。

剩下的我就没有研究了,但如果你是到google的网页然後点开F12你会发现会更不一样,有多了些东西,有兴趣的就自行上网研究。
今天就把Node.js介绍完,明天要介绍Python。


<<:  09. E2E Test x Browser Test x Cypress

>>:  Day 09 - Kbars 转换及储存至资料库

Day14 - 物理模拟篇 - 弹跳球世界IV(补完篇) - 成为Canvas Ninja ~ 理解2D渲染的精髓

没错~我就硬是不要给把标题打成『弹跳球世界V』,咬我啊~ 这篇是斜面碰撞的後篇~ 今天要来补完我们...

Day 10-不专业介绍package.jason

今天要来补充Day08的package.json的部分。 指路--->https://ithe...

Day 16 - Reverse String

大家好,我是毛毛。ヾ(´∀ ˋ)ノ 废话不多说开始今天的解题Day~ 344. Reverse St...

TailwindCSS 从零开始 - 把重复使用的功能变成元件

Utility-first 固然方便,也是有缺点的,虽然免除了定义 class 名称的困扰,但换来...

Day 01-Workshop Google Cloud Platform Get-Started

上一篇:Day 01-引言:Terraform 是个好东西 课程内容与代码会放在 Github 上:...