Michael Carneal
3 min readJul 22, 2019

--

Getting started with node.js: Creating a streamlined and lightweight backend

When I decided to begin my journey as a software developer, I opted to pursue training in the form of the Flatiron Web Development immersion course. During my time at Flatiron, I learned the basics of Sinatra, and how restful routing tied into the response request cycle. After learning the lay of the land, we eventually graduated to Ruby, and my programming life became much easier. When I graduated from flatiron, armed with an advanced understanding of these tools, and the request response cycle, I set out to build my first few web applications.

Over time, as I continued to develop with ruby, My projects felt bloated and I worried that they were not meeting their full potential. I was looking for a framework that would offer more flexibility and reduce the overhead in my projects.

Enter Node.js.

Node.Js is a lightweight environment that gives the author more flexibility to choose how your object relation mapping is handled as well as what types of databases you are pusing to. Ruby tends to be a handholding framework. Where as node give you the power to chose what works best for your needs.

Built on the google chrome v8 engine, The Node.js runtime environment is based on a event system. Once the environment is called, a perpetual loop is created where an active event listener is assigned to look for changes in the environment runtime. These changes could be anything from a URL change to an update in response from the API.

Unlike ruby on rails or sinatra, node requires a fine tuned approach to how your backend will be setup.

Below, I will give you a general overview of getting a node.js server started. Later in this series we will add express and abstract much of the logic to this process.

To begin, we need to create our environment.

First, create a new directory with a name that suits you, and create a .js file called app.

Next, log into your text editor of choice and lets starts writing some code.

The first thing you need to do is create a constant with our HTTP info.

const http = require(‘http’)

Then initialize your server and assign a port to listen to.

const routes = routes.reuire('./routes')
const server = http.createServer(routes)
server.listen(3000)

Now you have created primary classes and by doing this you are letting node know that you want to assign your response request cycle from sever to a particular function you have written.

Create a new file called routes .js.

In this file you are going to create two routes and parse the data the you are going to respond to.

const requestHandler = (req, res) => {
const url = req.url
const method = req.method
if (url === '/'){
res.setHeader('content-type', '
res.write("<h1>Greetings</h1>")
res.write("<body><form action='/create-user' method='POST'><input name='message' type='text'><button type='submit'>Send</button></fo
rm></body>")
return res.end()
}

if (url === '/create-user' && method === 'POST'){
const body = []
req.on('data', (chunk) => {
body.push(chunk)
})
return req.on('end', ()=> {
const parseBody = Buffer.concat(body).toString()
console.log(parseBody.split('=')[1])
res.statusCode = 302
res.setHeader('Location' , '/')
return res.end()
})
}
if (url==='/users'){
res.setHeader('content-type', 'text-html')
res.write("<body><ul><li>User One</li><li>User Two</li></ul></body>")
return res.end()
}
}
module.exports = requestHandler

With this code you have a create user route that will accept a form of user input data, and it will send it to a post route.

Your /users route will will display an unordered list of items with known user names.

And that’s it, you have successfully spun up a node.js server with two routes, given users a form and created an index page to view our data.

In the next post, we will go over this same process but abstract our logic with express to simplify our cycle.

--

--