Pzdrs
Pzdrs

Reputation: 41

NodeJS api path

I have a project in mind. I want to create an API in NodeJS for communicating with my database. I want to use that API in my Java application as well as in my website. When I use the API I use localhost:3000. How would I make it so the API responds on localhost:3000/projectName/api for example (localhost:3000/projectName would be the website - no idea if this is even the way to do this). I want to sort of prefix with the projectName/api. Is that possible?

Upvotes: 1

Views: 82

Answers (1)

kennysliding
kennysliding

Reputation: 2977

You can make all your api routes in an object, namely

apis = express.Router()

apis.get("/test", (req, res) => res.status(200))

app.use("/api", apis) // route all /api calls to here

app.get("/", (req, res) => res.status(500)) // ordering matters, block all / calls

Upvotes: 1

Related Questions