h t ttt t t p p
Us skinny-jean wearing, frappuccino slurpin chain gang of young folk like things homegrown and from scratch. You know things like, sourdough, kale, http servers.
Using a framework to sort out larger APIs and services is usually a good idea, but a lot of the times they do too much for what you actually need (hello it me, information overload, did you miss me?). We want you to try out building a server on your own.
exercise 1 - server
Your quest is to write up an http module to handle the usual suspects: req
, res
.
- start of by creating an
index.js
file - require
http
.http
API is exposed directly in node, so you don't need to npm-install it http.createServer()
should help you with most of the legwork of creating a server- attach a listener to a specific port so you can serve up your page
- intercept any given
req
and set the correctres
headers for ajson
withres.setHeader()
- set a status code to your response with
res.statusCode
- write
json
back with withres.write
(2) - don't forget to close the connection with
res.end()
- serve it with good ol' node
node ./index.js
- you can access your server with curl (or browser! capitalism tought us about
options after all) with
curl -i localhost:{port}
and get the body and headers back (1). You should get something that looks like this: - to 🍄 level up 🍄, look into
http.request()
exercise 2 - router
Think of how you might want to handle routing. Although http.request()
has a
path
option, looking at an abstraction is a good idea
- check out reg-router or server-router as your router options.
tipsy tips
(1) if you're using the browser to access your server, open up the network tab
to see your headers information. Network
> {yourRoute}
> Headers
. In the
same line of thinking, you can preview your response
under the same tab --
Network
> {yourRoute}
> Response
.
(2) altenatively you should look into res.writeHead()
to handle setting a
status code, sending back body, and setting headers.