Added config in .env file and added subpath

This commit is contained in:
Claudio Maggioni 2019-12-31 09:59:21 +01:00
parent c1d99a78b6
commit 45f6b8ea2b
4 changed files with 21 additions and 7 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules/ node_modules/
.env

View file

@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"node-fetch": "^2.6.0" "node-fetch": "^2.6.0"
} }

View file

@ -1,15 +1,20 @@
#!/usr/bin/env node #!/usr/bin/env node
// vim: set ts=2 sw=2 et tw=80: // vim: set ts=2 sw=2 et tw=80:
require('dotenv').config()
const http = require('http'); const http = require('http');
const url = require('url'); const url = require('url');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const express = require('express') const express = require('express')
const app = express() const app = express()
const port = 7000;
app.get('/contacts', (req, res) => { const { HOST, PORT, PREFIX, API } = process.env;
fetch('http://192.168.1.1/jrd/webapi?api=GetSMSContactList', {
app.get(PREFIX, (req, res) => res.redirect(PREFIX + '/contacts'));
app.get(PREFIX + '/contacts', (req, res) => {
fetch(API + '?api=GetSMSContactList', {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
@ -26,7 +31,8 @@ app.get('/contacts', (req, res) => {
<h1>Contatti dei messaggi</h1>`; <h1>Contatti dei messaggi</h1>`;
for (const m of e.result.SMSContactList) { for (const m of e.result.SMSContactList) {
html += `<h4><a href="/messages/${m.ContactId}">${m.PhoneNumber[0]}</a></h4> html += `<h4><a href="${PREFIX}/messages/${m.ContactId}">
${m.PhoneNumber[0]}</a></h4>
<p>${m.SMSContent}...</p>`; <p>${m.SMSContent}...</p>`;
} }
html += `</body></html>`; html += `</body></html>`;
@ -36,7 +42,7 @@ app.get('/contacts', (req, res) => {
.catch(e => e.status(400).json({ error: e.toString() })); .catch(e => e.status(400).json({ error: e.toString() }));
}); });
app.get('/messages/:id', (req, res) => { app.get(PREFIX + '/messages/:id', (req, res) => {
let id; let id;
if (!req.params.id || isNaN(id = parseInt(req.params.id))) { if (!req.params.id || isNaN(id = parseInt(req.params.id))) {
res.status(400).json({ error: `"${req.params.id}" is not a valid 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; const page = parseInt(req.query.page) || 0;
fetch('http://192.168.1.1/jrd/webapi?api=GetSMSContentList', { fetch(API + '?api=GetSMSContentList', {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
@ -84,4 +90,5 @@ app.get('/messages/:id', (req, res) => {
.catch(e => res.status(500).json({ error: e.toString() })); .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}!`));

View file

@ -75,6 +75,11 @@ destroy@~1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= 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: ee-first@1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"