Let me explain, I'm using node.js to run a web page locally, then I convert that page into an EXE using nativefier, I open the exe and everything works perfectly, the problem is that closing vscode shuts down the server, what makes my EXE no longer show the content by the route to which it points is to the address of said server.
My code to start the nodejs server is as follows:
const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
app.use(express.json());
app.use(express.static("express"));
app.use('/', function(req,res){
res.sendFile(path.join(__dirname+'/express/index.html'));
});
const server = http.createServer(app);
const port = 3000;
server.listen(port);
console.debug('Server listening on port ' + port);
Is there a way to automatically start the server after starting the EXE?
Related
I trying to open a local website on my local network that I can access from every devices in my network.
I use React as frontend, Nodejs as backend and mongodb is my database(Locally).
On the computer where everything is running I can enter the site through my address on the local network and see the data that is in the database, but as soon as I enter the site through another computer that is on the same network I see the site but not the data from the database.
How i can fix this ?
I use this code for the node js server and run node server in terminal:
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const ip = require('ip');
const app = express();
require('dotenv').config();
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri);
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB database connection established successfully');
});
const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');
app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);
app.use(express.static('../build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve('../build/index.html'));
});
const PORT = process.env.PORT || 3000;
console.log('server started on port:', PORT, ip.address());
app.listen(PORT,"0.0.0.0");
First thing is to specify the react HOST. If you're using create-react-app, Simply run HOST=your_ip npm run start.
To get this your_ip, run ifconfig on unix systems, and you'll find the IP for the your PC on the LAN you're connected to.
For the nodejs side, you might need to try something like
server.listen(80, 'current_local_ip');.
I've just started learning node.js but when I try to launch the hello world code by writing on the terminal node server.js, localhost:3000 gives me the following error:
This page isn’t working;
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
I've been searching for 2h now and I've found nothing. I've checked the port 3000 in cmd with netstat -a -n -o and it says that the port is listening (so I guess it is working). So what is preventing me from accesing to that port.
Here it my JS files:
server.js:
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer();
server.listen(port);
app.js:
const { request } = require('express');
const express = require('express');
const app = express();
app.use((req, res, next) => {
res.status(200).json({
message: 'It works'
});
})
module.exports = app;
Add the required app to the createServer call:
const server = http.createServer(app);
In this way the http server can route your http requests to it.
Could process.env.PORT be set somewhere, causing your app to be hosted on that port instead?
An easy way to check would be to console.log the PORT variable in your script, and see if it has any value.
If it does have a value, trying going to localhost:PORT
I have done npm install swagger-ui-express in the root folder of the project and kept a swagger.json as well there. My app.js contains:
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('./swagger.json');
const express = require('express');
const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// app.use('/api/v1', router);
const server = app.listen(3000, function () {
let host = "localhost"
let port = 3000
console.log("App listening at http://%s:%s", host, port)
})
I have created a folder api-docs as well inside the root project folder. When I run in terminal node app.js and see in the browser localhost:3000, it displays
Cannot GET /
What can be the issue?
I just started learning api development with nodejs, however, my node server wont start when i run node server.js. It doesn't display any errors.
Here is a screen shot
Here is the server.js file
const http = require('http');
const app = require('./app')
const PORT = process.env.PORT || 3000;
const server = http.createServer(app);
server.listen(PORT);
I am facing a problem connecting to a PhoneGap desktop application in my iPad.
I am using express to query to a MongoDB database which uses port 3000. However, PhoneGap desktop application also uses port 3000. If I change the port of my PhoneGap desktop application, I would be able to connect to the PhoneGap desktop application in my iPad. But, the query to MongoDB will not work.
How do I run both at the same time (being able to use in iPad)?
App.js:
var express = require('express');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/view');
app.set('view engine', 'jade');
app.use(express.bodyParser()) ;
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I tried using this method, the result is still the same.
var phonegap = require('connect-phonegap'),
express = require('express'),
app = express();
app.use(phonegap());
app.listen(3000);
please try the following code piece:
var server = app.listen(3000,'::1', function () {
var host = server.address().address;
var port = server.address().port;
console.log('running at http://' + host + ':' + port)
});
And try accessing the node process using the following URL:
http://[::1]:3000/
Phonegap should be accessible on the following URL:
http://localhost:3000
Let me know if this helps.