Home > other >  Why do we have to put api in front of routes?
Why do we have to put api in front of routes?

Time:12-22

I am learning express and the http methods, but I cannot find any documentation on it. Is /api/value just for the json data, like an address just for that data? Just any extra info on it would be appreciated. Like what exactly does it do and if there is any documentation from express about it. Or is this a global term used in urls throughout frameworks and the internet?

For example:

app.get('/api/jackets'(req, res) => {res.send('logic')})

Why do we need to add the api before jackets and what does it do?

CodePudding user response:

It's not necessary, it's used only for a better understanding

CodePudding user response:

The /api request is not required, but putting a prefix in front of the API requests such as:

/api

or, in some cases including a version number:

/api/v1

Allows you to use the same web server for more than one type of request because the /api prefix uniquely identifies each API request as an API request and it can easily be routed to where you handle API requests. You could have plain web page requests from a browser served by the same web server. While, you don't have to use such a prefix, using it gives you the most flexibility in how you deploy and use your server.

Similarly, putting the version in the prefix such as /api/v1 allows you to evolve your API in the future in a non-backward-compatible way by adding a new version designation without breaking prior API clients (you support both versions at the same time - at least for a transition period).

  • Related