Express framework — basic routing

Yu Che Liu
1 min readJan 25, 2021

Routing means it determines how an application responds to client request to specific URI or path including HTTP request method.

Photo by Lars Kienle on Unsplash

The basic structure of Route following the structure below:

app.METHOD(path, handler)

Where:

app is an instance of express.

METHOD is a HTTP request, including get, post, delete.

Path is a file path on the server side.

Handler is a function executed when the path is matched. In this function, it will return the HTML file or JSON formatted data back to user.

The basic route will be like this:

app.get(‘/’, (req, res)=>res.send(`Hello Express`))

When user enter / directory on their browser, it will execute the function (req, res)=>res.send(`Hello Express`).

Other routing response methods:

There are several response methods that we can use to response the users’ request.

res.download() => Prompt a file to be downloaded.

Res.json() => send a JSON response.

Res.redirect() => redirect a request to the other object.

Res.render() => render a HTML view template

Res.send() => send any response of different types.

Res.sendFile() => send a file

Res.sendStatus() => send the response status code, including 404, 200, 500.

--

--

Yu Che Liu

Java Full stack developer | Google Android Associate developer certified | CompTIA Linux + certified | Teaching Assistant | https://www.linkedin.com/in/yucheliu