Node React structure without view engine

342
March 18, 2017, at 07:51 AM

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?

Answer 1

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.

Rent Charter Buses Company
READ ALSO
How to create a “rebuild” task in gulp?

How to create a “rebuild” task in gulp?

In most case, "build" task doesn't need previous generated files to be removed (which slows the build process)

405
Node JS Instanciate classe A in classe B

Node JS Instanciate classe A in classe B

I'm beginning in nodejs and I just want to use my class B into my class A, but it seems not work

317
Node / Passport back-end working with Android?

Node / Passport back-end working with Android?

I've followed a simple Scotchio tutorial to build an authentication site set up using Node

376