Can I run 2 Node.js Project in windows? - javascript

Can I run 2 Node.js Project in windows?
If yes, How Can I do That?
If no, Can I run 2 Node.js Project in a dedicated Host?

You have a lot of options.
If you want to have diffrent versions of nodeJS, for windows NVM-windows is the best option.
But instead if you're talking about running different http-request based programs, the simplest solution is to simply listen at different ports on each of the project on your system.
Eg, if you're using nodejs http module
// project 1
server.listen('8080', (err) => { // Will start up the server on port 8080
console.log(`server is listening on 8080`)
})
// project 2
server.listen('8081', (err) => { // Will start up the server on port 8081
console.log(`server is listening on ${port}`)
})
or if you're using express server
// project 1
app.listen(3000, function() {
console.log('app listening on port 3000 for project 1 !');
})
// project 2
app.listen(3001, function() {
console.log('app listening on port 3001 for project 2 !');
})
NOTE: This requires that you switch to port 80 on server as that is the default port for request on any server.
If you'd want to get a more sophisticated solution wherein both these applications are sand-boxed into their own environment, you can go for Virtual box or Docker. Both of them offer same functionality but in different manner. They both can set up a isolated environment for your application so that you'r applications don't interact with each other.
To give you a perspective of this,
let's say your application uses a enviornment-variable that you have set to 'Abra-kadabra' for project 1. Now if you refrence that enviornment variable in project 2, you'd still get 'Abra-kadabra' while you might want second project to have the value 'whoosh'
Virualbox or docker would set up a isolated system where in you can have this exact functionality available to you

Related

Calling an API to run the code in the background

I have a node.js backend program that needs to be run by calling only the API. It doesn't have frontend or UI. The program is accessing my local machine for some excel files to be uploaded in the client's database.
In my local machine, I tried calling the API, (localhost:4000/${my_route}), through postman and the program successfully run. But when I uploaded my source code in Heroku and tried to run the created application by calling the API, (https://word-word-number.herokuapp.com/${my_route}), it doesn't work.
The expectation is, the client will just call the hosted API so that their excel files which is stored on their machine will go to their database.
Do you have any ideas where to host the application and how to call the API?
Update: Here is my index.js
Heroku doesn't allow you to choose a port so you should probably get it from the environmental variables.
Let's say you are using express you should do something like this
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

Moving ExpressJS API files onto Server

So I created an simple API using ExpressJS that connects to MongoDB to perform CRUD operations. Currently I am able to get the local host running by performing command "npm nodemon" in the source folder. And it worked by testing with postman I wonder how to implement it on the server. As server runs a linux system, also I have a line of code in my root file "server.js ":
const port = process.env.PORT || 5000;
I think the process.env.port needs need to be changed in order to make it work on the server?
In addition, I did look into aws CE2 server it is so complicated that I was immediately overwhelmed. I am hoping someone can recommend dummy like me a simple and very specific solution to have a server run my scripts in ExpressJS environment. Thank you
I'm assuming your question is "How to deploy express app to a server?"
You can read some advanced topics on http://expressjs.com/, which covers some best practices, and other useful stuff. But the things you want to look at now is Things to do in your environment / setup
The important part is:
Keep your express runing on port 5000
Run your app in cluster
Run your app behind a proxy server like Nginx.
You can check this nice guide (Step 3 and 4) on how to deploy your express app to a Linux server with PM2, Nginx.
So at the end, your express app will run on port 5000 (or whatever port you desire), and your Nginx will run on port 80, and nginx will forward any request to your express app.

how to listen on any port on heroku when a web port 80 is already in use?

I am new to heroku and i have deployed a meteorjs application on heroku. My Meteorjs application is a webapplication, so after the build is done it runs on heroku on port 80. But simultaneously i also want to listen on a port eg:4000 or any so that i can catch my webhook events fired by any third party i want to listen to. On my local machine it runs perfectly with webapp running on a port and listener running on another, but on heroku after i deploy its just the webapp which runs and listener doesn't listen. Can anybody help me or guide me. Any new solutions are also welcome..
In my server/main.js i have made a bot instance of facebook-bot-messenger and below is the code to start the listener
bot.listen(4000);
In my client/main.html i have the html code which is just hello world
On local machine when i visit http://localhost:3000 i can see helloworld and app is also listening on port 4000.
On heroku when i visit https://appname.herokuapp.com i see helloworld . In this case the port is 80 which is fine, but the bot is not listening as any post calls i make on https://appname.heroku.com on port 4000 doesn't respond.
Heroku assigns the port for you, so you can't set your own. You will have to read in the port number from the PORT environment variable. In Node this would be process.env.PORT
You can also use || for as and OR. For example in the below case, it will read in the port number from the environment variable while on Heroku, but locally it will default to port 4000.
const app = express();
app.set('port', (process.env.PORT || 4000));
//Start Server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});

How to get the runtime url of sails app within mocha tests

I'm aware of sails.getBaseUrl and the fact that it is deprecated. In the bootstrap.test.js while doing a sails lift I specify a port: 1337. sails.getBaseUrl() returns http://localhost:1337. I then run the tests using mocha (from within WebStorm if that matters). At the same time I'm able to do a sails lift at the terminal and run the same sails app on http://localhost:1337. Both seem to be running fine without a port conflict.
So at what location is mocha running the sails app when running the tests?
If you're not setting it to something else, then your Sails app is starting on port 1337. I'd check that you don't have a PORT environment variable set somewhere that one or the other app is using to override the default. Unless one of the apps is running in a virtual machine like Docker, it's not possible for both of them to be running on port 1337 without conflict, so either your tests are failing silently or they're running a different port.
In Sails v0.12.x and Sails v1.0, the HTTP server is available as sails.hooks.http.server, so you should be able to check the port that an app is listening on with sails.hooks.http.server.address().port.
The issue was that our tests were doing a sails load and not a sails lift. Based on the sails lift and load documentation and the source code I figured out that the port binding is not done in sails load. So I just added extra code for binding/unbinding the port as part of the test lifecycle.
before(function (done) {
server = sails.hooks.http.server.listen(sails.config.port, function (err) {
if (err) {
return done(err);
}
return done();
});
});
after(function (done) {
server.close();
done();
});

Running multiple httpServer in one Node Process or each server in own node process

I'm trying to figure out what's the better solution.
I am running two http servers in my node app, server 1 at port 3000, server 2 on port 3001. Server 1 is making the main logic and database handling, while server 2 handles the file requests.
When I start my app with node myApp.js both servers are launched and listening to their ports.
/*
*Fire Up the Servers
*/
http.createServer(app).listen(3000, function(){
console.log('HTTP Express server listening on port 3000');
});
http.createServer(fileserver).listen(3001, function(){
console.log('HTTP Fileserver is listening on port 3001');
});
Now my Question: Does anyone know if it would make any difference if I would write for every server an own node process, so that I have to run node myApp.js which launches server 1, listening on port 3000 and then run node myFileserver.js which listens on port 3001.
Is there any performance difference?
Or any hints where I can read something about it?
Regards,
Martin
Depends on the rest of the code. If both the servers have shared states it's better to start them in same process.
If there is no shared state it's better to have them in separate processes so that one's execution flow does not affect the number of requests server by the other one. Specially true if one is IO bound and the other one is cpu intensive.
Also if you are starting them in the same process why not do both the things on the same port?

Categories