You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
2.0 KiB
52 lines
2.0 KiB
3 years ago
|
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));
|
||
|
}
|