NodeJS搭建简易服务器

作者: 月半小夜曲_ | 来源:发表于2018-11-07 12:04 被阅读4次
    // http.js
    //引入内置http模块
    var http = require("http")
    // 创建服务器
    var server = http.createServer(function(req,res){
        // //设置响应状态码,响应头(编码格式)
        res.writeHead(200, {"Content-Type" : "text/plain; charset=utf-8"});
       //  //设置响应内容
        res.write("hello node.js!");
       //  //结束响应
        res.end()
    })
    
    // 设置服务器端口
    server.listen(3000)
    

    运行服务器

    在CMD命令窗口中切换到该服务器文件所在目录,然后 输入node http.js,回车!

    浏览器打开测试

    打开浏览器,在地址栏输入localhost:3000,回车!


    image.png

    原文详解:https://www.cnblogs.com/saber200/p/5242732.html

    相关文章

      网友评论

        本文标题:NodeJS搭建简易服务器

        本文链接:https://www.haomeiwen.com/subject/fuimxqtx.html