Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Viraj Nirbhavane
Viraj Nirbhavane

Posted on

     

Routes

A route is a code that represents an HTTP action,(GET, POST, PUT, DELETE). It has a URL endpoint, and a function that is used to handle when that endpoint is accessed.

We use postman to test these endpoints. Accessing these endpoints prior to setting up the code will return a 404 NOT FOUND - a html response.

Lets look at the code for a GET request at greeting endpoint.
We call the get method on the app object, passing in two arguments

  • The Endpoint.
  • The function to handle this endpoint. It takes in a request and a response object as parameters.
app.get('/api/greeting', (req, res) => {    res.send('Hello there, Welcome!')} )
Enter fullscreen modeExit fullscreen mode

Now, if we access the route, we get a 200 OK status and the String "Hello there...." is returned.

Usually we are gonna return JSON data. So the above code could be changed to

app.get('/api/greeting', (req, res) => {    res.status(200).json({message:'Hello there, Welcome!'})} )
Enter fullscreen modeExit fullscreen mode

We also set the status, although not necessary. Now we respond with a JSON object and the content type of the response changes from html to application/json. Even though we didn't add quotes on the key "message", it get parsed into JSON.


Now we don't wanna clutter our server.js file with all these routes. So we clean it up.

In the backend folder, we create a folder called 'routes' and in it we create file greetRoutes.js. So basically each resource in API will have it's own route file.

Folder structure : backend/routes/greetRoutes.js

Now to use the express router, we are gonna first bring the express into this file.

#greetRoutes.jsconst express = require('express');const router = express.Router();router.get('/', (req, res) => {    res.status(200).json({message:'Hello there, Welcome!'})} )module.exports = router
Enter fullscreen modeExit fullscreen mode

and inside server.js

#server.js....app.use('/api/greeting', require('./routes/greetRoutes.js');...
Enter fullscreen modeExit fullscreen mode

So now, if we hit /api/greeting, it's gonna look into the Route file, and all we needed was a / because /api/greeting is already specified.

Similarly, we can create other routes for POST, PUT, DELETE.
Some of them will require a :id in the endpoint part so that they access the right data point.router.put('/:id', (req,res) => {....})

Next up, Controllers. Again to clear the clutter!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Education
    RGIT, Mumbai University
  • Work
    Exploring React
  • Joined

More fromViraj Nirbhavane

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp