node从入门到放弃

node从入门到放弃

个人感觉我就是个吊毛 整体吊儿郎当的 干啥啥不会 整体乐呵呵 像个傻子 最近在学习node 分享下自己学到的那点东西 希望对你有帮助哦
后续会把《node入门》这本电子书发出来 写的还不错,学起来很轻松。

安装node

  • 顾名思义 学习node.js 那肯定的必须去安装node ,安装很简单,我知道对于一个前端来说,这个简直就是 so easy。

node安装地址

创建服务器 输入hello world

  • node.js作为运行在服务端的javascript 对于一个前端人来说 还是事关重要的,二话不说先甩出一个 Hello world!
  • 创建一个server.js 文件写入下面代码
var http = require("http");
    http.createServer((request, response) {
        response.writeHead(200, { "Content-Type": "text/plain" });
        response.write("hello world");
        response.end();
    }).listen(8888)
    console.log("start server...");

分析

  1. 引入http模块。
  2. 通过http.createServer()方法创建一个服务器,该方法返回一个回调函
    数,回调函数里面有request和response两个参数。
  3. 通过response.writeHead()方法设置状态码和Content-Type类型。
  4. 通过response.write();把hello world 响应给客户端。
  5. response.end()结束本次响应
  6. listen()方法监听8888 端口

以上代码 在命令行中通过 node server.js运行这个js文件。
在这里插入图片描述
打开浏览器 输入 http://localhost:8888 回车 你会发现 页面上出现hello world
在这里插入图片描述
到此为止一个简陋的node服务器也就算是搭好了。

猜你喜欢

转载自blog.csdn.net/weixin_43723051/article/details/124198199