How to reload the page in Express / nodejs

Asked

Viewed 497 times

3

I created a new project with Express and Nodejs, where I set a "/status" route. Each time this route is accessed, you must execute a routine in the script /routes/status.js. Example: If I run a F5 in the browser with the address http://192.168.137.2:3000/status, would have to execute the /routes/status.js, but this does not occur.

I imagine that’s the Express’s natural behavior. However, how do I get it executed in the way I described or otherwise?

2 answers

1

You are using some module to load your route, otherwise I indicate a so-called consign. You will install via npm

npm install --save consign

Import in your application

var consign = require('consign');

Define where the routes will be

consign()
.include('app/routes')
.into(app)

And you will normally create your current status.js by receiving

module.exports = function(app){
app.post('/status', function(req, res){
    //aqui vc direciona para sua view       
})

}

  • I don’t use "consign", but from what I understand in the documentation it’s something like the "nodemon" that I’m already using for the same function. But that is not what the problem in question is about. I am talking about a route that the user is opening in the browser, that is, a common address. When loading the page, the script for that route will be executed. But I don’t see it happening when I load the page.

0

If I understand the problem correctly, either you have not declared the route, or the route is calling the file incorrectly.

index js.

const app = express()
const status = require('./routes/status.js')

app.get('/status', status)

status js.

module.exports = (req, res, next) => {
    ... code ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.