Monday, September 11, 2017

How to successfully use express routing in electron project?

Leave a Comment

I am using ExpressJS in my Electron project. The routing with Express doesn't work as expected.

Here is how I created the routing (in the main process):

const express = require('express')  const app2 = express()  app2.get('/requests/:_id', (req, res, next) =>   {   console.log('Dynamic Link WORKS!!');   hosSchemaModel.findOne({ _id: req.params._id }, function(err, request){     res.json(request)     // res.sendFile(path.join(__dirname+'../homePage.html'))   }); }); 

And in the front-end I have the following:

<a href="/requests/{{this._doc._id}}">{{this._doc.status}}</a> 

When I click on {{this._doc.status}} the it takes me to empty white screen with nothing printed in the console.

Can I have some guidance on how to implement ExpressJS routing in Electron?

2 Answers

Answers 1

Just a shot in the dark but you won't be able to connect without a port. Try adding this to the end of your server file. 'app2.port(9000)` then try hitting the same URL but with a port.

Answers 2

Electron has basically two process main and rendered process, when you are printing console.log, it is basically printing in your main process's console. You have to pass data to renderer process to show in console of your web page.

UPDATE - 2

Make express sever to listen to some port, then from frontend hit the url having that port also.

Main.js

app2.get('/requests/1234', (req, res, next) =>   {   console.log('Dynamic Link WORKS!!');   hosSchemaModel.findOne({ _id: req.params._id }, function(err, request){     res.json(request);     // res.sendFile(path.join(__dirname+'../homePage.html'))   }); });  app2.listen(5000, function () {   console.log('Example app listening on port 3000!'); }); 

frontend

<a href="localhost:5000/requests/1234">{{this._doc.status}}</a> 

After this it is working at my end.

If you want to run the express server in cluster mode, you should fork the process and try running the express server in new process.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment