I want to simulate a crash with Node.js by using process.kill. I did a basic app with express.js in terminal #1
app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hi!');
});
const server = app.listen(3000, () => console.log('Server ready'));
console.log(process.pid);
process.on('SIGINT', function () {
console.log('Simulate crash');
});
Then, I'm running in another terminal (terminal #2) a node file that kill the process of terminal #1
kill.js
const fs = require('fs');
const args = process.argv.slice(2)[0];
process.kill(args, 'SIGINT');
So when I run kill.js with the following command $ node kill.js YOURPROCESSPID in the terminal #2, it didn't print the "simulate crash" in the terminal #1. I'd like to know how to make this happen. Thank you.
I also added the console down here.
Related
I created an express server, which also writes some text into a file I have in the project directory and executes a cmd command. Locally, it works, but if I deploy it to App Engine and try to make a request I get error 500.
If there isn't a way to do this, what could I do instead of it? I guess for fs, storing it in Cloud Storage, but I don't know about exec-
The code:
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { exec } = require('child_process');
var fs = require('fs')
const app = express();
app.use(bodyParser.json());
app.use(cors());
module.exports = app
app.post("/", (req,res) =>{
console.log("Get from /");
console.log(req.body.data)
fs.writeFileSync('../log.txt', req.body.data);
exec('npx hardhat run scripts/deploy.js --network goerli',
(error, stdout, stderr) => {
if (error !== null) {
console.log(`exec error: ${error}`);
}
else{
var addr = fs.readFileSync('../address.txt','utf8')
res.send(addr);
}
})
res.send(req.body.data);
});
// starting the server
app.listen(8080, () => {
console.log('listening on port 8080');
});
For App Engine Standard, you can't write to the local file system. You can write to /tmp which is a temporary directory. All files in this directory are stored in your instance's RAM which means you lose the file when the instance goes down (see documentation)
For App Engine Flexible, you can write to local disk but the disk is re-initialized whenever the VM is (re)started (see documentation)
Best thing would be to write to cloud storage
I have a relatively simple express setup along the lines of:
const app = express()
// ... some middleware registration
const server = app.listen(3000)
process.once('SIGINT', () => server.close())
I get an error: TypeError: server.close is not a function when nodemon restarts the process (I'm on windows, so I get SIGINT). I get the same error for closeAllConnections() for example.
What is the most puzzling is that some objects work as expected...
const server = ...
const socket = new WebSocket(...)
process.on('SIGINT', () => {
server.close() // dies
socket.send(...) // works
})
Does anyone have a clue what is going on with this combination of express/http, nodemon and javascript?
I need my server to go up, call a function then go down.
From what I've seen on the web I should use this section of code:
const express = require('express');
const app = express();
app.on('listening', () => {
console.log("Server up...");
console.log("Server going down...");
});
but for some reason this does not work for me.
The program does go up but the logs are not written.
const express = require("express");
const app = express();
const server = app.listen(3000);
//call whatever function you need here
server.close(() => console.log("Server going down..."));
First we start the server using app.listen and later when you want to close it just do server.close()
I'm following a YouTube tutorial on NodeJs, and for some reason my console.log commands unexpectedly stopped working. Here is the sample code from the tutorial as I've typed it:
const http = require('http');
const server = http.createServer();
server.on('connection', (socket) => {
console.log('New connection')
});
server.listen(3000);
console.log('Listening on port 3000...');
After saving I try using node and nodejs
node app.js
nodejs app.js
but they print nothing.
I would like to start/stop a second app with node.js
I have the following 2 nodejs applications in a directory:
--app.js
|
-app2.js
Inside app.js
serverProcess = spawn('node', ['app2.js']);
process.stdin.pipe(serverProcess.stdin);
serverProcess.stdout.on('data', data => {
console.log(`child stdout:\n${data}`);
});
setTimeout(function() {
console.log('kill');
serverProcess.stdin.pause();
serverProcess.kill();
}, 5000);
Inside app2.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
What I would like to do is run app.js which runs app2.js for 5 seconds, logs the output to stdout, so I can see it, then terminate the process.
Currently, app2.js is killed, but app.js continues running and it is not terminating.
How would I correct my code so that app.js terminates as soon as app2.js is killed?
The app.js won't exit when app2.js killed because it will listen for input of the stdio stream due to the line:
process.stdin.pipe(serverProcess.stdin);
You either have to write process.stdin.pause() (not serverProcess) after your console.log('kill').
Or remove the process.stdin.pipe(serverProcess.stdin) completely as you do not use the input you get via stdin.