Answers for "run express server on https"

53

express.js server

/* ====== create node.js server with express.js framework ====== */
// dependencies
const express = require("express");

const app = express();

app.get("/", (req, res) => {
   res.send("This is home page.");
});

app.post("/", (req, res) => {
   res.send("This is home page with post request.");
});

// PORT
const PORT = 3000;

app.listen(PORT, () => {
   console.log(`Server is running on PORT: ${PORT}`);
});


// ======== Instructions ========
// save this as index.js
// you have to download and install node.js on your machine
// open terminal or command prompt
// type node index.js
// find your server at http://localhost:3000
Posted by: Guest on January-13-2021
1

set node express server using ssl

const https = require("https"),
  fs = require("fs");

const options = {
  key: fs.readFileSync("/srv/www/keys/my-site-key.pem"),
  cert: fs.readFileSync("/srv/www/keys/chain.pem")
};

const app = express();

app.use((req, res) => {
  res.writeHead(200);
  res.end("hello world\n");
});

app.listen(8000);

https.createServer(options, app).listen(8080);
Posted by: Guest on June-20-2021
0

https with express

const fs = require('fs');
const https = require('https');

const app = require('express')();
app.get('*', (req, res) => res.send('<h1>Hello, World</h1>'));

const server = https.createServer({
  key: fs.readFileSync(`${__dirname}/localhost-key.pem`, 'utf8'),
  cert: fs.readFileSync(`${__dirname}/localhost.pem`, 'utf8')
}, app);

await server.listen(443);
Posted by: Guest on February-13-2021
0

create express server local

// create directory

//npm init -y
//npm i express --save

//create public directory
//create server.js

// <---- In the server js file --->

'use strict';

const express = require('express');
const app = express();
app.use(express.static('public'));// to connect with frontend html
app.use(express.json());//body parse

app.get('/', function(req,res){
	res.send('This is the Homepage');
  	//res.sendFile('index.html');
});

app.listen(3000);
Posted by: Guest on January-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language