2

I'm very new to node and have looked up how to simply set up a node server. I feel like I have it set up correctly but when I go to https://localhost:8080/ it says "Site can't be reached". Nothing is console logged either. I've gone through many similar questions but no solution has helped me yet. I ran npm init and npm install and here is my code:

var Express = require('express');
var Https = require('https');
var Fs = require('fs');

var app = Express();
var port = process.env.EXPRESS_PORT || 8080;
var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};
console.log("helloo?");

express.createServer(options, function (req, res) {
  console.log("hi")
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8080);

2 Answers 2

2

There are many typos in the code, to make it working i had done the changes.

To Create a https server you have to make use of built-in node.js https module and create a https server by passing your certificates, as below

GoTo - https://localhost:8080/

Response:

{ message: "this is served in https" }

var express = require('express');
var https = require('https');
var fs = require('fs');

var app = express();
var port = process.env.EXPRESS_PORT || 8080;
var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
}
console.log("helloo?");

app.get('/', function(req, res) {
    res.json({
        message: 'this is served in https'
    })
})

var secure = https.createServer(options, app); // for express

secure.listen(port, function() {
    console.log('localhost started on', port)
})

// for just node server request listener

/* https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(port); */
Sign up to request clarification or add additional context in comments.

7 Comments

So I copied what you have and I'm still not getting anything to console.log in terminal and I'm still getting "This site can’t be reached". Am I missing some crucial setup step? I'm just going to the file and running node server.js. I've created a package.json file and ran npm install.
Yea I'm going to that url. It doesn't even console.log "localhost started on"
did you restarted the server, use nodemon
Just installed nodemon and it says the following when I run server.js. After the below showed I entered 'rs' to restart. Still nothing is consoled and it says the site can't be reached. [nodemon] 1.11.0 [nodemon] to restart at any time, enter rs [nodemon] watching: . [nodemon] starting node server.js
This should just work fine, is the server setup file name is server.js please check,
|
0

I wrote a basic code for you. I think it will work for your answer and to you. try this. The other thing is if you are using express then there is no need to import the http module to connect to the server. I have commented some code ; try those one by one.

var express = require('express');
var app = express();
const fs = require('fs');
//const  http = require('http');

app.set('port',process.env.PORT || 3000);

app.get('/',function(req, res){
  res.send('Hellow World');
});

const fileName = __dirname + '/test.txt';
fs.readFile(fileName, (err, data) => {
   if (err) {
       console.error(err);
   }console.log('Done!');
   console.log(data.toString());
});

//or
/*
const data = fs.readFileSync(fileName);
console.log(data.toString());
*/

//below code will print whatever characters inside test.txt into test-copy.txt file
/*
const filename = __dirname + '/test.txt';
const outFileName = __dirname + '/test-copy.txt';

const readStream = fs.createReadStream(filename);
const writeStream = fs.createWriteStream(outFileName);

readStream.pipe(writeStream);

readStream.on('data', data => {
  console.log(data.toString());
});
*/
app.listen(app.get('port'), function(){
  console.log('Server listenning at port 3000');
});

First you have to create test.txt file and write something inside it. Next create tezt-copy.txt file if you try commented code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.