Last active
August 7, 2022 01:10
-
-
Save diegofcornejo/4a781dcbce316282b59ea450479c7bb9 to your computer and use it in GitHub Desktop.
PKG ExpressJS example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PORT=4000 | |
API_PROTOCOL=https | |
API_HOST=localhost | |
API_PORT=4001 | |
API_VERSION=v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PORT=3000 | |
API_PROTOCOL=http | |
API_HOST=localhost | |
API_PORT=3001 | |
API_VERSION=v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Logging Setup*/ | |
const log4js = require("log4js"); | |
let logger = log4js.getLogger(); | |
logger.level = 'ALL'; | |
/* Load application properties */ | |
const properties = require('dotenv').config({ path: './application.properties' }) | |
if (properties.error) { | |
// console.log(properties.error) | |
logger.warn('No application.properties file found') | |
logger.info('Use default application.properties') | |
require('dotenv').config({ path: './application.properties.default' }) | |
// throw result.error | |
} | |
/*Logs rotation*/ | |
const rfs = require("rotating-file-stream"); | |
const accessLogStream = rfs.createStream("access.log", { | |
size: "10M", // rotate every 10 MegaBytes written | |
interval: "1d", // rotate daily | |
// compress: "gzip" // compress rotated files | |
path: './logs' | |
}); | |
/*Server Setup*/ | |
const express = require('express') | |
const app = express() | |
const morgan = require('morgan') | |
const port = process.env.PORT || 3000 | |
const API = `${process.env.API_PROTOCOL}://${process.env.API_HOST}:${process.env.API_PORT}/${process.env.API_VERSION}` | |
app.use(morgan('combined', { stream: accessLogStream })) | |
app.get('/', (req, res) => { | |
logger.trace("Entering cheese testing"); | |
logger.debug("Got cheese."); | |
logger.info(API); | |
logger.warn("Cheese is quite smelly."); | |
logger.error("Cheese is too ripe!"); | |
logger.fatal("Cheese was breeding ground for listeria."); | |
res.send(`Hello World! ${API}`) | |
}) | |
app.listen(port, () => { | |
logger.info(`App listening on port ${port}`) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm i -g pkg | |
pkg -t node16-macos-x64 -o ../app --compress GZip index.js | |
#nohup | |
nohup ./app > app.log 2> app.err |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SERVICE_NAME=myservice | |
APP_PATH=$PWD | |
LOGS_PATH=$APP_PATH/logs | |
case "$1" in | |
start) | |
echo "Starting $SERVICE_NAME..." | |
mkdir -p $LOGS_PATH | |
nohup $APP_PATH/app 1> $LOGS_PATH/app.log 2> $LOGS_PATH/app-errors.log & | |
echo $!>$APP_PATH/$SERVICE_NAME.pid | |
echo $SERVICE_NAME started, pid=`cat $APP_PATH/$SERVICE_NAME.pid` | |
# echo $!>/var/run/$SERVICE_NAME.pid | |
# echo $SERVICE_NAME started, pid=`cat /var/run/$SERVICE_NAME.pid` | |
;; | |
stop) | |
echo "Stopping $SERVICE_NAME..." | |
kill `cat $APP_PATH/$SERVICE_NAME.pid` | |
rm $APP_PATH/$SERVICE_NAME.pid | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
status) | |
if [ -e $APP_PATH/$SERVICE_NAME.pid ]; then | |
echo $SERVICE_NAME is running, pid=`cat $APP_PATH/$SERVICE_NAME.pid` | |
else | |
echo $SERVICE_NAME is NOT running | |
exit 1 | |
fi | |
;; | |
*) | |
echo "Usage: $0 {start|stop|status|restart}" | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment