From 330e2176a6106380a6b093c8fecf743891d9a8b7 Mon Sep 17 00:00:00 2001 From: Juan Date: Thu, 2 Jun 2022 16:04:08 +0200 Subject: [PATCH] config via JSON --- b3-dev/node/session3/config.json/config.json | 24 +++++++++++++++++ b3-dev/node/session3/config.json/server.js | 40 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 b3-dev/node/session3/config.json/config.json create mode 100644 b3-dev/node/session3/config.json/server.js diff --git a/b3-dev/node/session3/config.json/config.json b/b3-dev/node/session3/config.json/config.json new file mode 100644 index 0000000..368b111 --- /dev/null +++ b/b3-dev/node/session3/config.json/config.json @@ -0,0 +1,24 @@ +{ + "development": { + "config_id": "development", + "app_name": "conf-test", + "app_desc": "An app to test environnement variables", + "node_port": 3000, + "json_indentation": 4, + "database": "nodesandbox_env-dev" + }, + "testing": { + "config_id": "testing", + "database": "nodesandbox_env-tst" + }, + "staging": { + "config_id": "staging", + "node_port": 8080, + "database": "nodesandbox_env-stage" + }, + "production": { + "config_id": "production", + "node_port": 8080, + "database": "nodesandbox_env" + } +} \ No newline at end of file diff --git a/b3-dev/node/session3/config.json/server.js b/b3-dev/node/session3/config.json/server.js new file mode 100644 index 0000000..0bceeae --- /dev/null +++ b/b3-dev/node/session3/config.json/server.js @@ -0,0 +1,40 @@ +const http = require('http'); +const url = require('url'); + +// load config file +const config = require(process.cwd() + '/config.json'); + +function outputHTML5(status, response, content, title) +{ + response.writeHead(status, { 'Content-Type': 'text/html; charset=utf-8' }); + response.write( + ` + + + + + ${title} + + + + ${content} + + + ` + ); + response.end(); +} + +var server = http.createServer(function (request, response) { + if(request.url === "/") { + + content = config.staging.config_id; + + outputHTML5(200,response,content,"Server info"); + } + else { + outputHTML5(404,response,'Y\'a rien ici',"Oups"); + } +}); + +server.listen(80);