MCPcopy
hub / github.com/typicode/json-server / start

Function start

src/cli/run.js:101–151  ·  view source on GitHub ↗
(cb)

Source from the content-addressed store, hash-verified

99 console.log(chalk.cyan(' \\{^_^}/ hi!'))
100
101 function start(cb) {
102 console.log()
103
104 console.log(chalk.gray(' Loading', source))
105
106 server = undefined
107
108 // create db and load object, JSON file, JS or HTTP database
109 return load(source).then((db) => {
110 // Load additional routes
111 let routes
112 if (argv.routes) {
113 console.log(chalk.gray(' Loading', argv.routes))
114 routes = JSON.parse(fs.readFileSync(argv.routes))
115 }
116
117 // Load middlewares
118 let middlewares
119 if (argv.middlewares) {
120 middlewares = argv.middlewares.map(function (m) {
121 console.log(chalk.gray(' Loading', m))
122 return require(path.resolve(m))
123 })
124 }
125
126 // Done
127 console.log(chalk.gray(' Done'))
128
129 // Create app and server
130 app = createApp(db, routes, middlewares, argv)
131 server = app.listen(argv.port, argv.host)
132
133 // Enhance with a destroy function
134 enableDestroy(server)
135
136 // Display server informations
137 prettyPrint(argv, db.getState(), routes)
138
139 // Catch and handle any error occurring in the server process
140 process.on('uncaughtException', (error) => {
141 if (error.errno === 'EADDRINUSE')
142 console.log(
143 chalk.red(
144 `Cannot bind to the port ${error.port}. Please specify another port number either through --port argument or through the json-server.json configuration file`,
145 ),
146 )
147 else console.log('Some error occurred', error)
148 process.exit(1)
149 })
150 })
151 }
152
153 // Start server
154 start()

Callers 1

run.jsFile · 0.85

Calls 2

createAppFunction · 0.85
prettyPrintFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…