OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Exit code 1 in simple express node.js server (VSCode)

  • Thread starter Thread starter Mydi - StudioGaming
  • Start date Start date
M

Mydi - StudioGaming

Guest
I'm using VSCode and created a small vue / express / node.js project.

While running my server I noticed that when I close it using ctrl+c in the VSCode terminal (power shell), there is a red circle with a cross next to the initial command line that when hovered tells me the command exited with an exit code of 1.

My assumption is that it should exit with a code of 0. Here is my code, run with node server.js

Code:
import express from "express";
import path from "path";
import { fileURLToPath } from 'url';
import http from 'http';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Our express app
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '../dist')));

app.get('/*', (req, res) => {
    res.sendFile(path.join(__dirname, '../dist/index.html'));
});

const server = http.createServer(app);

server.listen(8080, () => {
    console.log("Listening on port 8080...");
});

function shutdown() {
    console.log('Shutdown initiated...');
    server.close((err) => {
        if (err) {
            console.error('Error during server close:', err);
            process.exit(1); 
        } else {
            console.log('Server closed successfully');
            process.exit(0);
        }
    });

   // Force shutdown after 5 seconds if not closed
    setTimeout(() => {
        console.error('Force shutting down after timeout...');
        process.exit(1); 
    }, 5000);
}

// Handle termination signals
process.on('SIGTERM', () => {
    console.log('Received SIGTERM');
    shutdown();
});

process.on('SIGINT', () => {
    console.log('Received SIGINT');
    shutdown();
});

It logs:

Code:
Received SIGINT
Shutdown initiated...
Server closed successfully

When I run it in a terminal outside of VSCode, it just shows the above same logs but no error code mentioned.

Also, when, in VSCode, I add a Shutdown() right after the log of listening, it exits with a code of 0 properly.

I don't have any other interaction with my server (like asking for files, opened DB connection or anything else). I just open it / close it with ctrl+c.

Could it be just a VSCode issue or am I missing something? I'm not sure what else I could try to narrow down the issue.

I also added a block of code to display exit code used:

Code:
process.on('exit', (code) => {
    console.log(`Process exited with code: ${code}`);
});

And it displayed 0 both in VSCode (despite the red circle/arrow saying exit 1) and outside.

<p>I'm using VSCode and created a small vue / express / node.js project.</p>
<p>While running my server I noticed that when I close it using ctrl+c in the VSCode terminal (power shell), there is a red circle with a cross next to the initial command line that when hovered tells me the command exited with an exit code of 1.</p>
<p>My assumption is that it should exit with a code of 0. Here is my code, run with <code>node server.js</code></p>
<pre><code>import express from "express";
import path from "path";
import { fileURLToPath } from 'url';
import http from 'http';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Our express app
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '../dist')));

app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../dist/index.html'));
});

const server = http.createServer(app);

server.listen(8080, () => {
console.log("Listening on port 8080...");
});

function shutdown() {
console.log('Shutdown initiated...');
server.close((err) => {
if (err) {
console.error('Error during server close:', err);
process.exit(1);
} else {
console.log('Server closed successfully');
process.exit(0);
}
});

// Force shutdown after 5 seconds if not closed
setTimeout(() => {
console.error('Force shutting down after timeout...');
process.exit(1);
}, 5000);
}

// Handle termination signals
process.on('SIGTERM', () => {
console.log('Received SIGTERM');
shutdown();
});

process.on('SIGINT', () => {
console.log('Received SIGINT');
shutdown();
});
</code></pre>
<p>It logs:</p>
<pre class="lang-none prettyprint-override"><code>Received SIGINT
Shutdown initiated...
Server closed successfully
</code></pre>
<p>When I run it in a terminal outside of VSCode, it just shows the above same logs but no error code mentioned.</p>
<p>Also, when, in VSCode, I add a <code>Shutdown()</code> right after the log of <code>listening</code>, it exits with a code of 0 properly.</p>
<p>I don't have any other interaction with my server (like asking for files, opened DB connection or anything else). I just open it / close it with ctrl+c.</p>
<p>Could it be just a VSCode issue or am I missing something? I'm not sure what else I could try to narrow down the issue.</p>
<p>I also added a block of code to display exit code used:</p>
<pre><code>process.on('exit', (code) => {
console.log(`Process exited with code: ${code}`);
});
</code></pre>
<p>And it displayed 0 both in VSCode (despite the red circle/arrow saying exit 1) and outside.</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top