-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (36 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express')
const portfinder = require('portfinder')
const mdns = require('mdns');
const Config = require('./config')
const Weather = require('./weather')
const { json_status } = require('./utils')
// init our stuff
const settings = new Config()
// now we can build our modules
const weather = new Weather(settings)
// we need a port
let startPort = settings.port
portfinder.getPort({ port: startPort }, async (err, port) => {
// error
if (err || (startPort != null && port != startPort)) {
console.log(`Error: no available port found`)
process.exit(1)
}
// our server
const app = express()
app.use(express.json({limit: '50mb'}));
app.use('/', weather.routes())
// error handler
app.use((err, req, res, next) => {
console.error(err.stack)
json_status(res, err)
})
// start it
app.listen(port, () => {
// log
console.log(`Weather Text listening on port ${port}`)
// advertise
const ad = mdns.createAdvertisement(mdns.tcp('weathertext'), port);
ad.start();
})
})