mirror of
https://github.com/typicode/json-server.git
synced 2025-07-31 14:12:38 +08:00
Add switch for GZIP Content-Encoding
This commit is contained in:
@ -266,6 +266,7 @@ Options:
|
|||||||
--static, -s Set static files directory
|
--static, -s Set static files directory
|
||||||
--read-only, --ro Allow only GET requests [boolean]
|
--read-only, --ro Allow only GET requests [boolean]
|
||||||
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
|
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
|
||||||
|
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
|
||||||
--snapshots, -S Set snapshots directory [default: "."]
|
--snapshots, -S Set snapshots directory [default: "."]
|
||||||
--delay, -d Add delay to responses (ms)
|
--delay, -d Add delay to responses (ms)
|
||||||
--id, -i Set database id property (e.g. _id) [default: "id"]
|
--id, -i Set database id property (e.g. _id) [default: "id"]
|
||||||
|
@ -41,6 +41,10 @@ module.exports = function () {
|
|||||||
alias: 'nc',
|
alias: 'nc',
|
||||||
description: 'Disable Cross-Origin Resource Sharing'
|
description: 'Disable Cross-Origin Resource Sharing'
|
||||||
},
|
},
|
||||||
|
'no-gzip': {
|
||||||
|
alias: 'ng',
|
||||||
|
description: 'Disable GZIP Content-Encoding'
|
||||||
|
},
|
||||||
snapshots: {
|
snapshots: {
|
||||||
alias: 'S',
|
alias: 'S',
|
||||||
description: 'Set snapshots directory',
|
description: 'Set snapshots directory',
|
||||||
@ -69,6 +73,7 @@ module.exports = function () {
|
|||||||
.boolean('read-only')
|
.boolean('read-only')
|
||||||
.boolean('quiet')
|
.boolean('quiet')
|
||||||
.boolean('no-cors')
|
.boolean('no-cors')
|
||||||
|
.boolean('no-gzip')
|
||||||
.help('help').alias('help', 'h')
|
.help('help').alias('help', 'h')
|
||||||
.version(pkg.version).alias('version', 'v')
|
.version(pkg.version).alias('version', 'v')
|
||||||
.example('$0 db.json', '')
|
.example('$0 db.json', '')
|
||||||
|
@ -45,7 +45,8 @@ function createApp (source, object, routes, argv) {
|
|||||||
var defaultsOpts = {
|
var defaultsOpts = {
|
||||||
logger: !argv.quiet,
|
logger: !argv.quiet,
|
||||||
readOnly: argv.readOnly,
|
readOnly: argv.readOnly,
|
||||||
noCors: argv.noCors
|
noCors: argv.noCors,
|
||||||
|
noGzip: argv.noGzip
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv.static) {
|
if (argv.static) {
|
||||||
|
@ -19,7 +19,9 @@ module.exports = function (opts) {
|
|||||||
var arr = []
|
var arr = []
|
||||||
|
|
||||||
// Compress all requests
|
// Compress all requests
|
||||||
arr.push(compression())
|
if (!opts.noGzip) {
|
||||||
|
arr.push(compression())
|
||||||
|
}
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
if (opts.logger) {
|
if (opts.logger) {
|
||||||
|
@ -1,3 +1,28 @@
|
|||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
return { posts: [] }
|
return { posts: [
|
||||||
}
|
{
|
||||||
|
id : 1,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id : 2,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id : 3,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id : 4,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id : 5,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id : 6,
|
||||||
|
content : "Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
|
||||||
|
}
|
||||||
|
]}
|
||||||
|
}
|
@ -176,6 +176,60 @@ describe('cli', function () {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('db.json --no-gzip=true', function () {
|
||||||
|
|
||||||
|
beforeEach(function (done) {
|
||||||
|
child = cli(['fixtures/seed.js', '--no-gzip=true'])
|
||||||
|
serverReady(PORT, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not set Content-Encoding to gzip', function (done) {
|
||||||
|
var origin = 'http://example.com'
|
||||||
|
|
||||||
|
request.get('/posts')
|
||||||
|
.set('Origin', origin)
|
||||||
|
.expect(200)
|
||||||
|
.end(function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
done(err)
|
||||||
|
return
|
||||||
|
} else if ('content-encoding' in res.headers) {
|
||||||
|
done(new Error('Content-Encoding is set to gzip'))
|
||||||
|
} else {
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('db.json --no-gzip=false', function () {
|
||||||
|
|
||||||
|
beforeEach(function (done) {
|
||||||
|
child = cli(['fixtures/seed.js', '--no-gzip=false'])
|
||||||
|
serverReady(PORT, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should set Content-Encoding to gzip', function (done) {
|
||||||
|
var origin = 'http://example.com'
|
||||||
|
|
||||||
|
request.get('/posts')
|
||||||
|
.set('Origin', origin)
|
||||||
|
.expect(200)
|
||||||
|
.end(function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
done(err)
|
||||||
|
return
|
||||||
|
} else if ('content-encoding' in res.headers) {
|
||||||
|
done()
|
||||||
|
} else {
|
||||||
|
done(new Error('Content-Encoding is not set to gzip'))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
// FIXME test fails on OS X and maybe on Windows
|
// FIXME test fails on OS X and maybe on Windows
|
||||||
// But manually updating db.json works...
|
// But manually updating db.json works...
|
||||||
if (os.platform() === 'linux') {
|
if (os.platform() === 'linux') {
|
||||||
|
Reference in New Issue
Block a user