I'm new to node. I was using express-handlebars
as my view-engine
, but now I've added React and I understood that I no longer require handlebars. The problem that I'm having is that in order to get to the index.html
page, without handlebars, I had to use
app.use(express.static('./public'));
Everything gets rendered from react, but what if I want to do some other things when the user goes to the index page like
app.get("/",function(req,res){
console.log("connected");
});
If I add the get request after exporting the static files, the console.log
never gets called. If I use it before, it does get called, but I can see the page loading forever. How should I structure the application now that I'm using react and I don t have a view engine
anymore?
In your specific case, if you don't want to render anything to the user, you should turn your function into a middleware :
app.get("/",function(req,res, next){
console.log("connected");
next();
});
and put it before the app.use(express.static('./public'));
However, if you want to do actual logic with return values and such, I would suggest that you setup some kind of API that you request using Ajax from the client.
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
In most case, "build" task doesn't need previous generated files to be removed (which slows the build process)
I'm beginning in nodejs and I just want to use my class B into my class A, but it seems not work
I've followed a simple Scotchio tutorial to build an authentication site set up using Node