July 2017
Intermediate to advanced
656 pages
16h 1m
English
Let create a folder called json-validation, initialize it as a package, and create an index.js file:
$ mkdir json-validation$ cd json-validation$ npm init -y$ touch index.js
The index.js should look like so:
const http = require('http')const {STATUS_CODES} = httpconst server = http.createServer((req, res) => { if (req.method !== 'POST') { res.statusCode = 404 res.end(STATUS_CODES[res.statusCode]) return } if (req.url === '/register') { register(req, res) return } res.statusCode = 404 res.end(STATUS_CODES[res.statusCode])})function register (req, res) { var data = '' req.on('data', (chunk) => data += chunk) req.on('end', () => { try { data = JSON.parse(data) } catch (e) { res.end('{"ok": false}') return } // privileges ...