A small example to experience node.js

        Create a new folder --> Create a new serve.js file in the first-level directory of the new folder --> Write the following code in serve.js:

const http = require('http')

const hostname = '127.0.0.1'
const port = 3000

const server = http.createServer((req, res) => {
    res.statusCode = 200
    res.setHeader('Content-Type', 'text/plain')
    res.end('Hello World\n')
})

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`)
})

        Then enter the newly created folder directory in the cmd command window, and enter the node serve.js command to run the above code:

Then visit the obtained link ( http://127.0.0.1:3000/ )          in the browser to get the following effect:

 

Guess you like

Origin blog.csdn.net/m0_59778008/article/details/131553087