3

I am trying to setup a login system on a website.

In order to do that, I have to load http only cookies.

In order to load them, I have to send them back to the client via the response object in the createServer function when https starts up.

I have successfully done that via here: setting up https cookie in nodejs

The problem is twofold.

  1. The https server cookie only loads if I include the port number in the url. How do I get it to load without including the port number?

  2. When the server kicks in and loads the cookie, the static index.html that was always supposed to load on the client, doesn't load and instead all i get is what was written into the response object on the server. How do I get to load the cookie, and just load that static html file? I have tried sending in the entire html file as a respnose from the server side. But I'm not sure about that, plus i get MIME type problems in the browser.

john-jones
  • 7,490
  • 18
  • 53
  • 86

3 Answers3

3

I am not sure for the first part but for 2nd one,

  1. you have to properly mention about response data type in response header. so your should be something like this one:

    var app = express(); app.get('/test', function(req, res) { res.sendFile('views/test.html', {root:__dirname}) });

tech_amity
  • 192
  • 3
3

For your first part of the question "How do I get it to load without including the port number?" You can try creating virtual host e.g for localhost:3000 will be something.xyz.

And for your second part you need to serve index.html with render method as follow

 server.get('/', function (req, res) {
    res.render('index', { greeting: 'Welcome' });
});

Where index is you static file inside view directory.

Basit
  • 862
  • 1
  • 12
  • 30
  • https://ultimatefosters.com/hosting/setup-a-virtual-host-in-windows-with-xampp-server/ – Basit Sep 06 '20 at 14:50
  • I am not using express so there is no get() for me. I presume I should put it in the createserver function, as thats where i am making the resonse. but there i get: TypeError: res.render is not a function. – john-jones Sep 07 '20 at 09:17
  • should index not be index.html? – john-jones Sep 07 '20 at 09:17
  • is there any way to do the second part without express? – john-jones Sep 08 '20 at 08:03
  • should index not be index.html? The answer is No, in express you didn't specify file extension. You set the view engine while starting the server e.g 'ejs' – Basit Sep 08 '20 at 15:44
3

I've created a small demo, that might get you on the right track: https://github.com/bergur/simple-server-with-websocket

This example includes:

  1. https web server
  2. websocket server
  3. logic to get the cookies
  4. serving a temp.html file without express
  5. example of javascript class
  6. example of dependency injection in nodejs
Bergur
  • 3,962
  • 12
  • 20