Reputation: 219
I'm using NodeJS and I want to deploy my app.
When the app is on the server it to runs node ./bin/www
(standard configured by express) in package.json
, to start the server. But I want to run another script after the node ./bin/www
script is done ./api.js
(this script will loop forever).
The ./api.js
is located in the root folder of the projecr
This is what i tried but no succes so far.
"scripts": {
"start": "node ./bin/www",
"start": "node ./api.js"
}
The server launches both scripts, (checked by logs). But i cant access the website
LOGS:
> x@ start /app
> node ./api.js
State changed from starting to crashed
State changed from crashed to starting
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Stopping process with SIGKILL
www/bin:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('expressapp:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Upvotes: 1
Views: 509
Reputation: 985
Since your ./bin/www/
is a long-running process(An Express app that listens for TCP connections) and there is won't be an "end" for a typical long-running job.
I think the best way you could achieve such is to include you ./api.js
as a regular dependency
// ./bin/www
const app = express();
app.listen();
...code
require('./api')();
Upvotes: 0
Reputation: 1306
NPM has pre
and post
hooks in the scripts
section of package.json
to put scripts execution in sequence.
So, you can do:
"scripts": {
"start": "node ./bin/www",
"poststart": "node ./api.js"
}
to get what you whant. This solution is fully supported across systems.
Happy coding!
Upvotes: 1
Reputation: 762
The easiest way to do this would be to use the bash && operator, which runs one command then another once it is finished:
"scripts": {
"start": "node ./bin/www && node ./api.js"
}
Upvotes: 1