01node.js快速开始-node.js学习

01、下载并安装

下载地址:node.js


02、测试

在命令行中输入以下内容分别查看版本信息

node -v    
npm -v

03、hello world

新建hello.js文件,输入代码并保存

console.log("hello world")

打开命令行界面,执行代码

node hello.js

控制台将打印hello world


04、搭建一个服务器

修改js文件的代码为

//获取http模块
const http = require("http");

//创建server
http.createServer(function (request,response){
    
    
  //排除访问favicon.ico图标的请求
  if(request.url!='/favicon.ico'){
    
    
    response.writeHead(200,{
    
    
      'Cotent-Type':'text/html;charset=utf-8' //设置响应体的'Cotent-Type'为html文件
    });
    console.log("网络请求");
    response.write("hello world");
    response.end();   //响应体结束标志
  }
}).listen(8080);  //监听端口8080

重新运行,访问127.0.0.1:8080,会在控制台打印网络请求,并在页面上显示hello world。

猜你喜欢

转载自blog.csdn.net/qq_44856695/article/details/105116985