Files
iot/index.js
2020-04-09 12:12:05 +03:00

58 lines
1.5 KiB
JavaScript

// ./src/index.js
// importing the dependencies
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
var fs = require("fs");
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/zbor.eu.org/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/zbor.eu.org/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
// defining the Express app
const app = express();
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
// defining an array to work as the database (temporary solution)
const ads = [
{title: 'Hello, world (again)!'}
];
// adding Helmet to enhance your API's security
app.use(helmet());
// using bodyParser to parse JSON bodies into JS objects
app.use(bodyParser.json());
// enabling CORS for all requests
app.use(cors());
// adding morgan to log HTTP requests
app.use(morgan('combined'));
// defining an endpoint to return all ads
app.get('/', (req, res) => {
res.send(ads);
});
app.get('/dht', (req, res) => {
res.send("DHT");
});
// starting the server
//app.listen(3001, () => {
//console.log('listening on port 3001');
//});
// httpServer.listen(8080, () => {
// console.log('httpServer listening on port 8080');
// });
httpsServer.listen(8443, () => {
console.log('httpsServer listening on port 8443');
});