Shutting down several ports with an Express application

Currently, my app is running on multiple server instances on different ports. I achieve this by using the command "npm run node_1 & npm run node_2", which runs the app on localhost at ports 3001 and 3002. The issue arises when I attempt to close the application using CTRL+C, as it only closes the first port while leaving the other one active. This results in an error message indicating that the second port is still in use, as shown below.

[nodemon] starting

node ./networkNode.js 3002 http://localhost:3002
events.js:173 throw er; // Unhandled 'error' event ^

Error: listen EADDRINUSE: address already in use :::3002

My package.json :

"scripts": {
    "node_1": "nodemon --watch . -e js ./networkNode.js 3001 
           http://localhost:3001",
    "node_2": "nodemon --watch . -e js ./networkNode.js 3002 
           http://localhost:3002",
  },

Answer №1

If you place an ampersand (&) after a command, it will run as a background job. There are various commands that can help manage these jobs, which can be found here.

An easier solution might be to use the NPM package called concurrently to start these processes instead. When you stop concurrently using Ctrl+C, it will also end all the jobs it initiated.

Answer №2

When attempting to execute multiple processes using the "&" symbol in the command line, it typically results in only one process being attached to the terminal. In such cases, pressing "ctrl+c" will only trigger the SIGINT signal event for the attached process. For better management of multiple processes, it is recommended to use a process manager like pm2. Alternatively, you can opt for a lighter solution by utilizing the run-all package.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining Watson Assistant (formerly known as Conversation) with Telegram and Facebook

I'm feeling a bit lost when it comes to connecting my Watson bot with messaging platforms like Facebook Messenger. I created the bot using the Watson Conversation service and deployed it on a Bluemix Cloud Foundry node.js app. While it's working ...

A guide on ensuring all necessary keys are present in a JSON document using Node.js

I have been researching various methods for efficiently checking if a key exists in a JSON file. The challenge I am facing is related to optimizing this process. My current workflow involves users uploading a CSV file which I then convert into JSON format ...

Error: npx is unable to locate the module named 'webpack'

I'm currently experimenting with a customized Webpack setup. I recently came across the suggestion to use npx webpack instead of node ./node_modules/webpack/bin/webpack.js. However, I am encountering some difficulties getting it to function as expecte ...

What command should be used as an alternative to "npm start" for this specific scenario?

The following snippet is from my package.json file on my computer. "scripts": { "postinstall": "bower install", "start": "http-server -p 8000", } Executing npm start successfully starts the http server. However, when I try to run node http-serve ...

Having trouble reaching express endpoints with axios while using snowpack and react

I'm currently developing a web application using Express, Snowpack, and React all within the Node.js environment. While I can successfully load the view, I'm encountering difficulties when trying to make requests to specific endpoints. Below is ...

When attempting to add or store data in MongoDB, it triggers a 500 server error

Greetings, I am currently working on developing a CRUD app using the MEAN stack. The Express application loads successfully and retrieves the "contactlist" collection from the database. However, when attempting to make a POST request to "/api/contacts", an ...

Error in Svelte/Typescript: Encounter of an "unexpected token" while declaring a type

Having a Svelte application with TypeScript enabled, I encountered an issue while trying to run it: [!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) src\api.ts (4:7) 2: 3: export default class API { 4: ...

OpenTok Angular 6 encountered an error with code TS2314 stating that the generic type 'Promise<T>' needs to have 1 type argument specified

Issue in opentok.d.ts File: Error TS2314 npm version: 6.2.0 node: v8.10.0 Angular CLI: 6.2.3 Operating System: Linux x64 Angular Version: 7.0.0-beta.5 @opentok/client": "^2.14.8 ...

Having trouble with the updateOne() method in MongoDB - it's not updating my document nor displaying any errors. What could be the issue?

I'm currently facing an issue where I am attempting to update a user's document in the database with a value obtained from a calculator. However, despite not encountering any errors, the document does not seem to be updating and the page just con ...

Retrieving and managing IDs in MongoDB using Node.js without using the ObjectId

In my Node.js code, when I append a customer with an ID to my cart, it appears like this: "customer" : ObjectId("5755251e4e2210ce2f953407") Is there a way to have the output as follows? "customer" : "5755251e4e2210ce2f953407" function verifyLogin(req, ...

Using `npm audit --force` is advised only for those with expertise. If you are unsure, what steps should you take? Could my application be vulnerable while

As a newcomer to Angular, I recently ran into the usual warnings when executing npm install: found 42 vulnerabilities (40 moderate, 2 high) run `npm audit fix` to fix them, or `npm audit` for details After running npm audit fix, only a few vulnera ...

Issue: The error message "articales.forEach is not a function" is indicating

app.get('/', (req, res) => { const articales = { title: 'Test Articles', createdAt: Date.now(), description: "Test Description" } res.render('index', { articales : articales }) }) <div ...

Is a single connection sufficient in Node.js when using the Mongoose driver to handle concurrent requests in MongoDB?

Can a single connection pool size of { poolSize: 1 } be used for multiple simultaneous web service calls in MongoDB? Will the connection be reused or will it result in an exception? I am using the Mongoose driver in Node.js with MongoDB as the database. ...

Issues with triggering the success block in AngularJS and Node.js Express when using $http.get

As a beginner in the world of AngularJS and Node.js, I'm facing an issue with my $http.get method. The problem is that the success callback block does not get executed when the request is successful, whereas the error callback works just fine when the ...

What could be preventing React from successfully uploading images to the server?

My current project involves using React and Express on the backend, with Multer handling uploads. While my server side functions correctly when testing with Postman, I am encountering unexpected results when trying to upload an image from React. Despite su ...

What is the importance of using sequelize 'seed'?

Can someone explain the seed concept in the sequelize npm package to me? I have not been able to find information on this in any tutorials, and I am curious about why we need SEED. Thank you! ...

Reload the MEN stack webpage without the need to reload the entire page

I am in the process of developing a data analytics dashboard using the MEN stack (MongoDB, Express.js, Node.js). I have successfully implemented functionality to display real-time data that refreshes every 5 seconds without the need to reload the entire ...

Unable to transmit an object using ExpressJS

Greetings. I am currently trying to comprehend ExpressJS. My goal is to send a simple object from the express server, but it only displays "cannot get" on the screen. app.get("/", (req, res, next) => { console.log("middleware"); const error = true; ...

How do I locate a single entry based on the _id filter and modify it using Node.js and MongoDB with Mongoose?

While attempting to update a student record in MongoDB using Node.js, I utilized the findOneAndUpdate method with the filter being the studentId. The 'student' variable represents my schema instance containing the document to be updated. However, ...

What is the reason browsers do not store JWT tokens in the same manner as they do cookies?

Understanding the distinctions between a cookie and a JSON web token is crucial. With cookies, when the server includes 'set-cookie' in its response header, the browser automatically stores the cookie in local storage. Then, whenever the cookie i ...