~mna/funweb

51cff7f9073c999dee3caa8399c3468542f917fa — Martin Angers 1 year, 1 month ago e04b198
add cors
3 files changed, 17 insertions(+), 2 deletions(-)

M lib/funweb.js
M package-lock.json
M package.json
M lib/funweb.js => lib/funweb.js +7 -2
@@ 3,10 3,12 @@ const path = require('path')
const R = require('ramda')
const serveStatic = require('serve-static')
const compression = require('compression')
const cors = require('cors')
const pug = require('pug')
const logger = require('./logger')

// need some automatic generation of forms/html from code (can be fully controlled with templates or jsx, or auto-generated using a distinct library)
// TODO: make the canonical middleware signature (next, req, res) so it can be curried more easily? Or even (next, [req, res])?

// status and body helper
const handler = (status, body) => R.pipe(


@@ 48,6 50,9 @@ const routes = R.cond([
const logAndNext = (req, res) => logger(req, res, R.thunkify(routes)(req, res))
const compress = compression()
const compressAndNext = (req, res) => compress(req, res, R.thunkify(logAndNext)(req, res))
// TODO: configure CORS properly for the application's needs
const corsFn = cors()
const corsAndNext = (req, res) => corsFn(req, res, R.thunkify(compressAndNext)(req, res))

// start server
const logAddress = R.pipe(


@@ 56,5 61,5 @@ const logAddress = R.pipe(
  R.join(':'),
  (addr) => console.log(`listening on ${addr}...`)
)
const server = http.createServer(compressAndNext)
server.listen(9000, '127.0.0.1', () => logAddress(server))
const server = http.createServer(corsAndNext)
server.listen(3000, '127.0.0.1', () => logAddress(server))

M package-lock.json => package-lock.json +9 -0
@@ 1060,6 1060,15 @@
      "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=",
      "dev": true
    },
    "cors": {
      "version": "2.8.5",
      "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
      "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
      "requires": {
        "object-assign": "^4",
        "vary": "^1"
      }
    },
    "coveralls": {
      "version": "3.0.3",
      "resolved": "https://registry.npmjs.org/coveralls/-/coveralls-3.0.3.tgz",

M package.json => package.json +1 -0
@@ 11,6 11,7 @@
  "license": "ISC",
  "dependencies": {
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "pug": "^2.0.3",
    "ramda": "^0.26.1",
    "serve-static": "^1.14.1"