Node.js for Newbies: Learn to Build Your First Web Service

Kenny B Cheng
Digital Products Tech Tales
3 min readJun 9, 2023
Node.js, baby.

Node.js is a great tool for building web services, especially for people starting out. The ease at which you can stand up an API from nothing is not to be underestimated. Of course, as you scale up your application, there will always be languages, tools and frameworks that make enterprise level development easier, but that’s a story for another time.

JavaScript can be thought of as the lifeblood of the web, and Node.js is an extension of that, allowing you to power your JavaScript front-ends with JavaScript back-ends.

So with that being said, let’s get started.

First things first is to download Node.js

If you’re using iOS or Linux, you’re probably aware of more straight-forward ways of installing Node through things like homebrew or apt.

Get your terminal up. Next, you’re gonna want to check that your Node is installed and ready to use.

node --version

You should get back a version, such as so

v20.0.0

Next, we’ll make our project files, use the following commands

mkdir projects

This will make the folder where your Node.js application will live in. Go into the folder using

cd projects

Create your app file using

touch app.js

Next, up we’re gonna install an external library, Express, which is Node’s most popular, lightweight API framework. We do that through the Node Package Manager, or npm. So run the code as below.

npm i express

And it should generate a node_modules folder, as well as, a package-lock.json and a package.json file.

 projects
├── app.js
└── node_modules
└── package-lock.json
└── package.json

With all the setup out of the way, we can start building.

Open your app.js file in an IDE or text editor. The following code imports the express library (for creating APIs) and then we instantiate an app variable that we can build our endpoints on top of.

const express = require('express');

const app = express();

Next up, we add endpoints onto the app. These endpoints are hit with HTTP requests and then send back data to the client. In this case, the data being sent back is that the “API is up and running.”

app.get('/', (req, res) => {
res.send('API is up and running');
})

Lastly, we want to stand up our web server itself and make sure it’s continuously running to listen to requests.

app.listen(3080, () => {
console.log('App has started up')
})

And that’s pretty much it. It’s that easy to start a web service in Node.js.


//app.js file

const express = require('express');

const app = express();

app.get('/', (req, res) => {
res.send('API is up and running');
})

app.listen(3080, () => {
console.log('App has started up')
})

Since, the coding is all finished, we can finally get it up and running.

In your terminal, run

node app.js

This should start up your server and keep it up, until you decide to shut it down with Ctrl + C.

In a web browser, you can now go to

//3080 since in our app.js, it's listening on this port

http://localhost:3080/

And you should now see the page displaying

API is up and running

So, that’s pretty much the basics. Setting up a Node.js web server is super quick and easy. There’s of course so much more to explore developing a fully fleshed out API, like ORMs, middleware, routing, services, schema validation, etc., but Node.js makes starting out incredibly straightforward.

To continue on your Node.js journey.

For building a more robust

Images taken from

https://www.wallpaperflare.com/node-js-hexagon-green-hole-illustration-computers-others-programming-wallpaper-plpnh

--

--