The following code
var http = require('http')
var fs = require('fs')
var url = require('url')
var port = process.argv[2]
if(! port) {console.log('Please enter port number \n for example: node server.js 8888')
process.exit(1)}var server = http.createServer(function (request, response) {
var parsedUrl = url.parse(request.url, true)
var pathWithQuery = request.url
var queryString = ' '
if (pathWithQuery.indexOf('? ') > =0) { queryString = pathWithQuery.substring(pathWithQuery.indexOf('? '))}var path = parsedUrl.pathname
var query = parsedUrl.query
var method = request.method
/******** start here, don't look at ************/ above
console.log('Request received! The request path (with query parameters) is: ' + pathWithQuery)
if (path === '/') {
response.statusCode = 200
response.setHeader('Content-Type'.'text/html; charset=utf-8')
response.write(`
Hello Node.js
`)
response.end()
} else if (path === '/style.css') {
response.statusCode = 200
response.setHeader('Content-Type'.'text/css; charset=utf-8')
response.write(`h1{color: red; } `)
response.end()
} else {
response.statusCode = 404
response.setHeader('Content-Type'.'text/html; charset=utf-8')
response.write(The page you visited does not exist)
response.end()
}
/******** code ends, do not read ************/
})
server.listen(port)
console.log('listen' + port + 'Success \n Please use your browser to open http://localhost:' + port)
Copy the code