Question:
I created a new project with Express and Nodejs, where I defined a "/status" route. Each time this route is accessed, it must run a routine in the /routes/status.js
script. Example: If I run an F5 in the browser with the address http://192.168.137.2:3000/status
, I would have run /routes/status.js
, but that doesn't happen.
I imagine this behavior is natural to Express. But how do I get it to run in the way I've described or any other way?
Answer:
Are you using some module to load your route, otherwise I indicate a consign one. You will install via npm
npm install --save consign
It will import in your application
var consign = require('consign');
Define where the routes will be
consign()
.include('app/routes')
.into(app)
And it will normally create its status.js file receiving
module.exports = function(app){
app.post('/status', function(req, res){
//aqui vc direciona para sua view
})
}