From 45f6b8ea2b420c4083354b87813b9bb238f51836 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 31 Dec 2019 09:59:21 +0100 Subject: [PATCH] Added config in .env file and added subpath --- .gitignore | 1 + package.json | 1 + server.js | 21 ++++++++++++++------- yarn.lock | 5 +++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index c2658d7..713d500 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.env diff --git a/package.json b/package.json index 04aa681..62b3d3b 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "dotenv": "^8.2.0", "express": "^4.17.1", "node-fetch": "^2.6.0" } diff --git a/server.js b/server.js index 47645a4..68c9e18 100755 --- a/server.js +++ b/server.js @@ -1,15 +1,20 @@ #!/usr/bin/env node // vim: set ts=2 sw=2 et tw=80: +require('dotenv').config() + const http = require('http'); const url = require('url'); const fetch = require('node-fetch'); const express = require('express') const app = express() -const port = 7000; -app.get('/contacts', (req, res) => { - fetch('http://192.168.1.1/jrd/webapi?api=GetSMSContactList', { +const { HOST, PORT, PREFIX, API } = process.env; + +app.get(PREFIX, (req, res) => res.redirect(PREFIX + '/contacts')); + +app.get(PREFIX + '/contacts', (req, res) => { + fetch(API + '?api=GetSMSContactList', { method: 'POST', body: JSON.stringify({ jsonrpc: "2.0", @@ -26,7 +31,8 @@ app.get('/contacts', (req, res) => {

Contatti dei messaggi

`; for (const m of e.result.SMSContactList) { - html += `

${m.PhoneNumber[0]}

+ html += `

+ ${m.PhoneNumber[0]}

${m.SMSContent}...

`; } html += ``; @@ -36,7 +42,7 @@ app.get('/contacts', (req, res) => { .catch(e => e.status(400).json({ error: e.toString() })); }); -app.get('/messages/:id', (req, res) => { +app.get(PREFIX + '/messages/:id', (req, res) => { let id; if (!req.params.id || isNaN(id = parseInt(req.params.id))) { res.status(400).json({ error: `"${req.params.id}" is not a valid id` }); @@ -45,7 +51,7 @@ app.get('/messages/:id', (req, res) => { const page = parseInt(req.query.page) || 0; - fetch('http://192.168.1.1/jrd/webapi?api=GetSMSContentList', { + fetch(API + '?api=GetSMSContentList', { method: 'POST', body: JSON.stringify({ jsonrpc: "2.0", @@ -84,4 +90,5 @@ app.get('/messages/:id', (req, res) => { .catch(e => res.status(500).json({ error: e.toString() })); }); -app.listen(port, () => console.log(`Example app listening on port ${port}!`)); +app.listen(PORT, HOST, + () => console.log(`SMSReader listening on port ${HOST}:${PORT}!`)); diff --git a/yarn.lock b/yarn.lock index 8a23231..04efa88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -75,6 +75,11 @@ destroy@~1.0.4: resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= +dotenv@^8.2.0: + version "8.2.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a" + integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"