ataehee

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

引用nodejs内置http模块,创建http server。

 

// 直接引用内置http模块,直接写模块名即可
const http = require('http')

// 使用ES6规范,使用箭头函数
const server = http.createServer((req,res) =>{
    res.writeHead(200, {'content-type' : 'text/html'})
    res.end('<h1> hello world!</h1>')
})

server.listen(3000, () => {
    console.log("listen on port 3000")
})

成功运行后,在浏览器访问http://localhost:3000,即可正常访问刚刚创建的网页。

此外,还有更为精简版本

// Lite version
const http = require('http')
const server = http.createServer((req, res) => {
    res.end('Hello world, Lite!')
})

server.listen('3000')

 

此外,使用VSCODE,熟悉其内置git和debug功能

posted on 2020-09-13 07:01  ataehee  阅读(186)  评论(0编辑  收藏  举报