简体   繁体   中英

How to create simple web server with HTTP/1.0 protocol in Node.js

I've homework for Client Server Programming course and it want me to create Multi-Client Web Servers. But the rule is web server must implement HTTP version 1.0 protocol, where separated HTTP requests are sent for each component of the Web Page. Unfortunately, I'm just know to work with Node.js. I know C, but it long times (around 10 years ago) and only do very basic programming such as arithmetic operation, work with string and arrays, not OOP in C.

So the question, how to create web server with HTTP/1.0 protocol in Node.js?

Currently, I have node v.10.15.1 installed on my laptop (using macos). I've tried with http and net module but can't find how to configure the protocol to use HTTP/1.0

We need to require the http module first and bind our server to the port which we listen on. Inside the index.js :

    // content of index.js
const http = require('http')
const port = 3000

const requestHandler = (request, response) => {
  console.log(request.url)
  response.end('Hello Its Your Node.js Server!')
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

So lets start it with:

$ node index.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM