I am currently learning how to create servers with Node js and other Javascript apis, and I've built a both HTTP and HTTPS server which works fine on every pc navigator of my nat network but when I try the https server on my android mobile device with: https://192.168.1.20:8443 which is where I built my node.js server it won't load and say the page is not working although it works with http.
I would also like to know if I could make my server visible or access to it with my router public ip address and how could I do that.
This is my server code:
var fs = require('fs');
var http = require('http');
var https = require('https');
var cors = require('cors');
var privateKey = fs.readFileSync('host.key', 'utf8');
var certificate = fs.readFileSync('host.cert', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.use(cors());
app.use('/work', express.static('/.'))
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
console.log('Servidor en marxa.');
});
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(8080);
httpsServer.listen(8443);
Some hours later I have just realized I needed to forward my router ports to where I have my Node.js server built. I had to port forward both the HTTP (#80) and HTTPS (#443) to grant the access by public ip address.
Also had to modify part of my Javascript Server code.
var fs = require('fs');
var http = require('http');
var https = require('https');
var cors = require('cors');
var privateKey = fs.readFileSync('host.key', 'utf8');
var certificate = fs.readFileSync('host.cert', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.use(cors());
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
console.log('Servidor en marxa.');
});
// your express configuration here
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(80,"0.0.0.0");
httpsServer.listen(443,"0.0.0.0");
var io = require("socket.io")(httpServer);
Related
I'm trying to get HTTPS working on express.js for node, and it won't run.
This is my server.js code.
const fs = require('fs');
const http = require ('http');
const https = require('https');
const options = {
pfx: fs.readFileSync('ssl/pfxfile.pfx'),
passphrase: 'password'
};
const express = require('express');
const app = express();
const path = require('path');
app.use(express.json());
app.use(express.static("express"));
app.use('/', function(req,res){
res.sendFile(path.join(__dirname+'/express/index.html'));
});
var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);
httpServer.listen(8080);
httpsServer.listen(8443);
When I run it reports no errors but it just get stuck to nothing (I waited 30 minutes to see if it does something and nothing happened).
httpServer.listen(8080, ()=>{console.log('Server is running')});
If the server successfully started, it should output "Server is running" in the console. This is a nice way to check if the server is working as intended.
I found my error, thanks for your answers, it's been helping me, my error was first that I didn't put any console.log and the second was that I was not typing 8443 in the browser.
const fs = require('fs');
const http = require('http');
const https = require('https');
const options = {
pfx: fs.readFileSync('ssl/pfxfile.pfx'),
passphrase: 'password'
};
const express = require('express');
const app = express();
const path = require('path');
app.use(express.json());
app.use(express.static("express"));
app.use('/', function(req,res){
res.sendFile(path.join(__dirname+'/express/index.html'));
});
const httpServer = http.createServer(app);
const port = process.env.PORT || 8080;
const httpsServer = https.createServer(options, app);
const portHttps = process.env.PORT || 8443;
httpServer.listen(port, () => console.log('Http listening on port ' + port));
httpsServer.listen(portHttps, () => console.log('Https listening on port ' + portHttps));
const https = require("http");
const app= require('./app');
const port = 8080;
const server = https.createServer(app);
server.listen(port)
I am not sure of what you were trying to do but you can use express in this way:
var express = require('express')
var app = express()
app.get('/', function (req, res) {
/// do something here
})
app.listen(8080)
Also, check express guide : http://expressjs.com/en/starter/hello-world.html
I am beginner to nodejs. I tried below code. But server is not listening to the port and host.
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config.host;
var port = config.port;
var express = require("express") ,http = require("http");
var app = express();
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
app.listen(port, host);
I also tried below code. But found in SO that this method is deprecated. And I replaced it with above code. Though it is not working. I also tried installing express globally then also not listening to port. What might be the problem.
var express = require("express");
var app = express.createServer();
app.get('/', function(request, response){
response.send("hello");
});
app.listen(port, host);
This is my config file.
[{
"host" : "127.0.0.1",
"port" : 1337
}]
config is array not object, you need to access it accordingly(or change the config) .Here is corrected code;
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config[0].host;
var port = config[0].port;
var express = require("express") ,http = require("http");
var app = express();
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
var server = app.listen(port, host);
server.on('error', function(err) {
console.log('error:' + err);
});
server.on('listening', function(){
console.log('server is up, all is well');
});
I made a simple node.js server which shows index.html. When I run it on localhost it shows the page but when I try to run it on http:ip:8000 it says "This web page is not available".
This is my code:
// server2.js (Express 4.0)
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser()); // pull information from html in POST
app.use(methodOverride()); // simulate DELETE and PUT
var router = express.Router();
var notes = [
{username: 'Mohit1406', password: 'midkm' ,firstName: 'mxkz', lastName: 'ckzmk', dob: '1994-05-08', gender: 'male'},
];
router.get('/note', function(req, res) {
res.send(notes);
});
router.post('/note', function(req, res) {
var note = req.body;
notes.push(note);
res.send(note);
});
app.use('/api', router);
app.listen(8000,'0.0.0.0');
console.log('Open http://localhost:8000 to access the files now'); // shoutout to the user
app.listen(8000);
hostname is optional
server.listen(port, [hostname], [backlog], [callback])#
Begin accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).
new to node development.
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
my content gets as below
server.listen(1337);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
I am trying to get static file as below
server.use("/", app.static(__dirname + '/'));
but it doesnt wonk getting errors.
How to get staTIC files?
just read express docs
var express = require('express'),
app= express(),
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
app.use("/", express.static(__dirname + '/'));
the static function is a method on the express module.
So it should be:
var express = require('express'),
app = express(),
io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/'));