-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
38 lines (32 loc) · 1009 Bytes
/
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
require('@babel/register')
const post = require('./lib/postHandler')
const http = require('http')
const URL = require('url')
const resp = require('./lib/response').default
const debug = require('debug')('lambda-form')
http.createServer((req, res) => {
if (req.method !== 'POST') {
return resp(res, null)(`Invalid request method: ${req.method}`, 422)
}
const url = URL.parse(req.url, true)
const ctype = req.headers['Content-Type'] | req.headers['content-type']
// parse query string
req.queryStringParameters = url.query
// parse form id
const path = url.pathname.replace(/^\/*|\/*$/g, '')
req.pathParameters = {
id: path.split('/')[0]
}
if (ctype.indexOf('application/json') > -1) {
let data = ''
req.on('data', (chunk) => { data += chunk })
req.on('end', () => {
req.rawBody = data
post(req, res, null)
})
} else {
return post(req, res, null)
}
}).listen(process.env.PORT || 5000, () => {
debug('Listening for requests');
});