Brian Oh
Brian Oh

Reputation: 10720

react.js test if node server running

I'm learning React.js. My React.js app and Node Server use:

Server: MongoDb, Mongoose, Express, Cors, Axios.
React.js app: BrowserRouter, Switch, Route, Link.

When my React.js app starts, I want to test if the Node Server is running before making any database requests.

How can I best do that? (use ping somehow?)

Upvotes: 0

Views: 2602

Answers (1)

Ryan Brunner
Ryan Brunner

Reputation: 14851

There's not really a way to tell if a server is up without attempting to communicate with that server. But it's relatively simple to set up an endpoint on your server that will tell you whether it's running. Just set up a simple route in express that unconditionally sends a successful status code. If you get a response from that, you know at least that Express is running:

app.get('/status', (req, res, next) => res.sendStatus(200));

Upvotes: 5

Related Questions