How do I separate my REST API from my Express web app?

Kamuela Franco

I have an Express app serving a front-end web app.

How do I create an API server with /api as its root? I would like to separate the API concerns from the main app.

nickbdyer

There are likely to be considerations over the intensity of connections to your API. That will be one of the leading factors in determining whether you really need a separate server.

In answer to your domain query, there are npm packages available to assist in creating an api subdomain if thats suitable https://api.example.com, but assuming it's a small to medium sized application, you'll be fine to use Express Router to achieve what you want. Details are here in the docs.

app.use('/api', router);

This will apply a filter to all requests so that only those with /api will reach this router like:

https://www.example.com/api/users/1

In fact, there could be an argument against prematurely optimising your app and building a second server. That said, if you modularise your code base suitably, then it should be a breeze to transfer the api routes over to a new server later down the line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I tweet from my bot account when the bot app is created under a separate account?

From Dev

How do I return a string from a Buffer from a mysql blob field in my Express API?

From Dev

How do I optimise this callback hierarchy in my simple express app?

From Dev

How can I pull in more WordPress posts from my angular app, using the WordPress REST api

From Dev

How can I pull in more WordPress posts from my angular app, using the WordPress REST api

From Dev

How do I retrieve my custom variables from a Bamboo Atlassian Build Plan via REST API

From Dev

How do I allow users to authenticate with my REST API?

From Dev

How do I pass values from my express middleware function?

From Dev

How can I host my API and web app on the same domain?

From Dev

How do I import a library from the lib folder in my web app?

From Dev

How do I know what format the token is in that gets sent from ADFS to my web app?

From Dev

How do I open any app from my web browser (Chrome) in Android, automatically?

From Dev

How do I get web analytics on traffic to my firebase app?

From Dev

How do I keep my Android testing libraries separate from my application libraries in Android Studio?

From Dev

How do I return the HTTP Status Code from a method in my Web API to main?

From Dev

How do I pass the data from MongoDB to my web api through node.js?

From Dev

How do I include my php.ini file in my web app at Google Web Engine?

From Dev

How do I get the resource ID of the app icon in my applicaton from an API

From Dev

How do I get the App Engine Discovery document for my API?

From Dev

How do I separate my component and my service?

From Dev

Should I use separate Docker containers for my web app?

From Dev

How do I stop my android app from crashing in the emulator?

From Dev

How do I call REST API from an android app?

From Dev

I want to give access to users of their own dropbox from my java web app. How can I do that?

From Dev

How do I make my published web app that is installed on my IIS be accessed by another computer

From Dev

How can I define my REST API?

From Dev

How can I access my local REST api from my android device?

From Dev

How do I display an image that I just took from my camera in my android app?

From Dev

How do I run my app on real iPhone? (i have a certificate from my coworker)

Related Related

  1. 1

    How do I tweet from my bot account when the bot app is created under a separate account?

  2. 2

    How do I return a string from a Buffer from a mysql blob field in my Express API?

  3. 3

    How do I optimise this callback hierarchy in my simple express app?

  4. 4

    How can I pull in more WordPress posts from my angular app, using the WordPress REST api

  5. 5

    How can I pull in more WordPress posts from my angular app, using the WordPress REST api

  6. 6

    How do I retrieve my custom variables from a Bamboo Atlassian Build Plan via REST API

  7. 7

    How do I allow users to authenticate with my REST API?

  8. 8

    How do I pass values from my express middleware function?

  9. 9

    How can I host my API and web app on the same domain?

  10. 10

    How do I import a library from the lib folder in my web app?

  11. 11

    How do I know what format the token is in that gets sent from ADFS to my web app?

  12. 12

    How do I open any app from my web browser (Chrome) in Android, automatically?

  13. 13

    How do I get web analytics on traffic to my firebase app?

  14. 14

    How do I keep my Android testing libraries separate from my application libraries in Android Studio?

  15. 15

    How do I return the HTTP Status Code from a method in my Web API to main?

  16. 16

    How do I pass the data from MongoDB to my web api through node.js?

  17. 17

    How do I include my php.ini file in my web app at Google Web Engine?

  18. 18

    How do I get the resource ID of the app icon in my applicaton from an API

  19. 19

    How do I get the App Engine Discovery document for my API?

  20. 20

    How do I separate my component and my service?

  21. 21

    Should I use separate Docker containers for my web app?

  22. 22

    How do I stop my android app from crashing in the emulator?

  23. 23

    How do I call REST API from an android app?

  24. 24

    I want to give access to users of their own dropbox from my java web app. How can I do that?

  25. 25

    How do I make my published web app that is installed on my IIS be accessed by another computer

  26. 26

    How can I define my REST API?

  27. 27

    How can I access my local REST api from my android device?

  28. 28

    How do I display an image that I just took from my camera in my android app?

  29. 29

    How do I run my app on real iPhone? (i have a certificate from my coworker)

HotTag

Archive