From 28018c32c041c79ebb25272fa957bc8078e7b69d Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Fri, 13 Nov 2015 11:42:16 +0000 Subject: [PATCH] CORS --- lib/local-web-server.js | 4 +++- test/{static => fixture}/.local-web-server.json | 0 test/{ => fixture}/ajax.html | 4 ++-- test/{static => fixture}/big-file.txt | 0 test/{static => fixture}/file.txt | 0 test/{static => fixture}/something.php | 0 test/test.js | 8 ++++---- 7 files changed, 9 insertions(+), 7 deletions(-) rename test/{static => fixture}/.local-web-server.json (100%) rename test/{ => fixture}/ajax.html (83%) rename test/{static => fixture}/big-file.txt (100%) rename test/{static => fixture}/file.txt (100%) rename test/{static => fixture}/something.php (100%) diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 3933263..552bfc6 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -7,6 +7,7 @@ const serveIndex = require('koa-serve-index') const morgan = require('koa-morgan') const compress = require('koa-compress') const streamLogStats = require('stream-log-stats') +const cors = require('kcors') /** * @module local-web-server @@ -31,7 +32,8 @@ function getApp (options) { const app = new Koa() - // CORS + /* CORS: allow from any origin */ + app.use(convert(cors())) if (options.mime) { app.use((ctx, next) => { diff --git a/test/static/.local-web-server.json b/test/fixture/.local-web-server.json similarity index 100% rename from test/static/.local-web-server.json rename to test/fixture/.local-web-server.json diff --git a/test/ajax.html b/test/fixture/ajax.html similarity index 83% rename from test/ajax.html rename to test/fixture/ajax.html index ee6bbce..3430c61 100644 --- a/test/ajax.html +++ b/test/fixture/ajax.html @@ -9,10 +9,10 @@ - \ No newline at end of file + diff --git a/test/static/big-file.txt b/test/fixture/big-file.txt similarity index 100% rename from test/static/big-file.txt rename to test/fixture/big-file.txt diff --git a/test/static/file.txt b/test/fixture/file.txt similarity index 100% rename from test/static/file.txt rename to test/fixture/file.txt diff --git a/test/static/something.php b/test/fixture/something.php similarity index 100% rename from test/static/something.php rename to test/fixture/something.php diff --git a/test/test.js b/test/test.js index 31ad10d..4346346 100644 --- a/test/test.js +++ b/test/test.js @@ -41,7 +41,7 @@ test('static', function (t) { const app = localWebServer({ log: { format: 'none' }, static: { - root: __dirname + '/static', + root: __dirname + '/fixture', options: { index: 'file.txt' } @@ -57,7 +57,7 @@ test('serve-index', function (t) { const app = localWebServer({ log: { format: 'none' }, serveIndex: { - path: __dirname + '/static', + path: __dirname + '/fixture', options: { icons: true } @@ -74,7 +74,7 @@ test('compress', function(t){ const app = localWebServer({ compress: true, log: { format: 'none' }, - static: { root: __dirname + '/static' } + static: { root: __dirname + '/fixture' } }) launchServer(app, { headers: { 'Accept-Encoding': 'gzip' } }, '/big-file.txt', response => { t.strictEqual(response.res.headers['content-encoding'], 'gzip') @@ -85,7 +85,7 @@ test('mime', function(t){ t.plan(1) const app = localWebServer({ log: { format: 'none' }, - static: { root: __dirname + '/static' }, + static: { root: __dirname + '/fixture' }, mime: { 'text/plain': [ 'php' ]} }) launchServer(app, null, '/something.php', response => {