-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (61 loc) · 1.95 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}))
const compression = require('compression')
app.use(compression({
threshold: 0,
level: 9,
memLevel: 9
}))
// CORSを許可する
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
const fs = require('fs')
const path = require('path')
const NeDB = require('nedb')
const lib = require('./asset/library')
const showTime = () => {
const time = new Date()
const z = (v) => {
const s = '00' + v
return s.substr(s.length - 2, 2)
}
return time.getFullYear() + '/' + (time.getMonth() + 1) + '/' + time.getDate() + ' ' + z(time.getHours()) + ':' + z(time.getMinutes()) + ':' + z(time.getSeconds())
}
const checkDB = (id) => {
try {
fs.statSync(path.join(__dirname, '/asset/database/' + id + '.db'))
return true
} catch (error) {
if (error.code === 'ENOENT') return false
}
}
const createDB = (id) => {
return new NeDB({
filename: path.join(__dirname, '/asset/database/' + id + '.db'),
autoload: true
})
}
app.get('/', function(req, res) {
res.send('Demo: https://postalcode.netlify.com')
})
app.post('/api', (req, res) => {
const query = req.body.query
console.log('[' + showTime() + '] /api: ' + query)
if (query.match(/^[0-9]{3}-?[0-9]{4}$/g)) {
const postalCode = query.replace(/-/g, '')
if (!checkDB(postalCode.slice(0, -4))) return res.json({error: {type: 'notFound'}})
createDB(postalCode.slice(0, -4)).find({postalCode}, (error, doc) => {
doc.map((each) => delete each._id)
if (error || doc.length === 0) return res.json({error: {type: 'notFound'}})
return res.json({postalCode, address: doc})
})
} else {
return res.json({error: {type: 'notMatch'}})
}
})
app.listen(process.env.PORT || 3000)