Skip to content

Add convenience cli paramter --anyInterface #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,25 @@ json-server db.json --middlewares ./first.js ./second.js
json-server [options] <source>

Options:
--config, -c Path to config file [default: "json-server.json"]
--port, -p Set port [default: 3000]
--host, -H Set host [default: "localhost"]
--watch, -w Watch file(s) [boolean]
--config, -c Path to config file [default: "json-server.json"]
--port, -p Set port [default: 3000]
--host, -H Set host [default: "localhost"]
--anyInterface, -a Binds to any network interface addresses (eq. to 0.0.0.0) [boolean]
--watch, -w Watch file(s) [boolean]
--routes, -r Path to routes file
--middlewares, -m Paths to middleware files [array]
--middlewares, -m Paths to middleware files [array]
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [boolean]
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
--snapshots, -S Set snapshots directory [default: "."]
--read-only, --ro Allow only GET requests [boolean]
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
--snapshots, -S Set snapshots directory [default: "."]
--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"]
--foreignKeySuffix, --fks Set foreign key suffix, (e.g. _id as in post_id)
[default: "Id"]
--quiet, -q Suppress log messages from output [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
[default: "Id"]
--quiet, -q Suppress log messages from output [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]

Examples:
json-server db.json
Expand Down
19 changes: 19 additions & 0 deletions __tests__/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const tempWrite = require('temp-write')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const serverReady = require('server-ready')
const os = require('os')

let PORT = 3100

Expand Down Expand Up @@ -88,6 +89,24 @@ describe('cli', () => {
})
})

describe('Server endpoint', () => {
const hostName = os.hostname()
beforeEach(done => {
child = cli(['-a', true, dbFile])
// serverReady(PORT, hostName, done)
setTimeout(done, 1000)
})

test('should answer using os.hostname address', done => {
let customRequest = supertest(`http://${hostName}:${PORT}`)
customRequest.get('/').expect(200, done)
})

test('should answer using localhost address', done => {
request.get('/').expect(200, done)
})
})

describe('seed.js', () => {
beforeEach(done => {
child = cli(['../../__fixtures__/seed.js'])
Expand Down
5 changes: 5 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module.exports = function() {
description: 'Set host',
default: 'localhost'
},
anyInterface: {
alias: 'a',
description: 'Binds to any network interface address (like 0.0.0.0)',
default: false
},
watch: {
alias: 'w',
description: 'Watch file(s)'
Expand Down
4 changes: 4 additions & 0 deletions src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ module.exports = function(argv) {

// Create app and server
app = createApp(db, routes, middlewares, argv)
if (argv.anyInterface) {
// listen on all interfaces
argv.host = '0.0.0.0'
}
server = app.listen(argv.port, argv.host)

// Enhance with a destroy function
Expand Down