parent
fec08957d5
commit
3ec83fa400
@ -0,0 +1,6 @@
|
||||
log*.txt
|
||||
log.txt
|
||||
*.log
|
||||
.env
|
||||
.env.*
|
||||
**/package-lock.json
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"dotenv": "^16.0.1"
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
const http = require('http')
|
||||
const url = require('url');
|
||||
// fs = filesystem
|
||||
const fs = require('fs');
|
||||
// utils
|
||||
var util = require('util');
|
||||
|
||||
// si on veut conserver les logs d'un instance à l'autre on "append"
|
||||
// var logfile = fs.createWriteStream(process.cwd() + '/server.log', { flags: 'a' });
|
||||
// si on veut reste on "write"
|
||||
var logfile = fs.createWriteStream(process.cwd() + '/server.log', { flags: 'w' });
|
||||
console.log = function () {
|
||||
logfile.write(util.format.apply(null, arguments) + '\n');
|
||||
process.stdout.write(util.format.apply(null, arguments) + '\n');
|
||||
}
|
||||
|
||||
const server = http.createServer(function (request, response) {
|
||||
parsedUrl = url.parse(request.url, false);
|
||||
json_response(200,{status : "active"},response);
|
||||
});
|
||||
|
||||
//cwd = current working dir
|
||||
var default_env_file = process.cwd()+'/.env';
|
||||
console.log(default_env_file);
|
||||
var local_env_file = process.cwd()+'/.env.'+process.env['NODE_ENV'];
|
||||
console.log(local_env_file);
|
||||
// j'utilise fs pour savoir si j'a le droit de lire mon fichier env local
|
||||
fs.access(local_env_file, fs.constants.R_OK, (err) => {
|
||||
//si je n'est pas le droit
|
||||
if(err) {
|
||||
console.log('loading : '+ default_env_file);
|
||||
require('dotenv').config({ path: default_env_file });
|
||||
}
|
||||
//si ok charger l'environnement
|
||||
else {
|
||||
console.log('loading : '+ local_env_file);
|
||||
require('dotenv').config({ path: local_env_file });
|
||||
}
|
||||
console.log('server port ? '+ process.env.SERVER_PORT);
|
||||
console.log('DB name ? '+ process.env.DB_NAME);
|
||||
server.listen(process.env.SERVER_PORT || 8080);
|
||||
datetime=new Date();
|
||||
console.log('server started at :'+datetime.toLocaleString('fr-FR', {hour12: false}));
|
||||
console.log('server started at :'+datetime.toLocaleString('en-EN'),'server started at :'+datetime);
|
||||
});
|
||||
|
||||
//usefull functions
|
||||
function json_response(code, object, response) {
|
||||
response.statusCode = 200;
|
||||
response.setHeader('content-type', 'Application/json');
|
||||
response.end(JSON.stringify(object));
|
||||
}
|
Loading…
Reference in new issue