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
- 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}/`);
});
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');
});
When our express app gets a get
-request
to /
, it send
s a response
of Hello World
.
We can see all relevant words immediately.
Every route in express has the same syntax:
app.METHOD(PATH, HANDLER)
- 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 aget
-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}/`);
});
Run it from the terminal
- Run it:
node index.js
- Console Result:
Server running at: http://localhost:8080/
Visit: http://localhost:8080/welcome
- Client Result:
Further Reading
Your Task
- Create a basic route, that handles a
GET
-request to/dashboard
, thatresponds
withThis 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)
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)
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 :-)
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!
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 ๐
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.
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.
Nice one, that looks exactly like what I was looking for!
Cheers!