DEV Community

miku86
miku86

Posted on • Updated on

NodeJS: How To Add Routes To Our Express Server

Intro

So we installed NodeJS on our machine.

We also learned how to create a simple server using express.

Now we want to learn how to add routes to our express server.

Reuse our simple server from the last article

  • Open your terminal
  • Create a file named index.js:
touch index.js
Enter fullscreen mode Exit fullscreen mode
  • Add this JavaScript code into it:
const express = require('express');
const app = express();

const PORT = 8080;

app.get('/', (request, response) => {
  response.send('Hello World');
});

app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});
Enter fullscreen mode Exit fullscreen mode

Note: This is our starting point, we have one working route.
I updated req to request & res to response to increase readability.


Basic Routing

What is Routing?

Every time a client (e.g. the user's browser) requests an app's endpoint, the server has to respond.

The client sends a specific HTTP request method, e.g. GET, and a path, e.g. /.

To respond to this request, our express app needs to know how to respond.

Example from above:

app.get('/', (request, response) => {
  response.send('Hello World');
});
Enter fullscreen mode Exit fullscreen mode

When our express app gets a get-request to /, it sends a response of Hello World.

We can see all relevant words immediately.

Every route in express has the same syntax:

app.METHOD(PATH, HANDLER)
Enter fullscreen mode Exit fullscreen mode
  • METHOD: Which HTTP request method was sent by the client? (e.g. GET)
  • PATH: Which path does the client request? (e.g. /, /account, /dashboard)
  • HANDLER: How should the app respond to the request? (e.g. send data back, redirect, log something)

Add a new route

  • Add a new route to /welcome for a get-request:
const express = require('express');
const app = express();

const PORT = 8080;

app.get('/', (request, response) => {
  response.send('Hello World');
});

app.get('/welcome', (request, response) => {
  response.send('Welcome!');
});

app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});
Enter fullscreen mode Exit fullscreen mode

Run it from the terminal

  • Run it:
node index.js
Enter fullscreen mode Exit fullscreen mode
  • Console Result:
Server running at: http://localhost:8080/
Enter fullscreen mode Exit fullscreen mode

Visit: http://localhost:8080/welcome

  • Client Result:

Alt Text


Further Reading


Your Task

  • Create a basic route, that handles a GET-request to /dashboard, that responds with This is your dashboard
  • If you want to get some feedback, I invite you to share your code in the comments (beginner) or on Github (advanced)

Top comments (8)

Collapse
 
thecodingalpaca profile image
Carlos Trapet

I was thinking about this the other day and I'm looking for input from other developers, and I can't find an answer I like;

How would you handle routing if your service had a large amount of exposed routes? (By "large" I mean between 50 to 100)

Collapse
 
hilleer profile image
Daniel Hillmann

I would suggest creating multiple routers with express.Router() each handling it's own route, e.g. /api and /app.
If you have multiple depth levels you could create additional routers and attach to your express application.

If it has interest and you want further explanation I can try to write a post about it :-)

Collapse
 
thecodingalpaca profile image
Carlos Trapet • Edited

Yeah that sounds like the cleanest solution, probably. And build each route/app/section in its own containerised module.

Yeah if you write a post let me know and I'll happily read it! :)
Cheers man!

EDIT: ah, you live in Copenhagen <3 my favourite city in the world. Hope to some day move there, sooner, rather than later!

Thread Thread
 
hilleer profile image
Daniel Hillmann • Edited

Exactly!

Sure, will do :-)

Yes, it indeed is a decent city - and there seem to be a high demand for developers of many different flavours. Just saying šŸ˜‰

Thread Thread
 
thecodingalpaca profile image
Carlos Trapet

Yeah, I did some browsing not too long ago and there seem to be some Node/React/JS roles advertised, although not a great deal.
And the entry barrier seems really high, so I assume the demand for devs isn't as high as here in London.

Thread Thread
 
hilleer profile image
Daniel Hillmann

I see.

I can't compare between the two, as I have no knowledge about the demand in London (or other places in general than Copenhagen). I can suggest you to keep and eye on thehub.dk/ or signup for their weekly job emails - there does come interesting Node/React/JS posts from time to time.

Thread Thread
 
thecodingalpaca profile image
Carlos Trapet

Nice one, that looks exactly like what I was looking for!
Cheers!

Collapse
 
nomangul profile image
Noman Gul

const express = require("express");
const app = express();

app.get("/dashboard", (_, res) => 
  res.send("This is your cool dashboard"));

app.listen(4000, () => console.log("server running..."))