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/
.env

View File

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

View File

@ -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) => {
<h1>Contatti dei messaggi</h1>`;
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>`;
}
html += `</body></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}!`));

View File

@ -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"