json-server
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -22,3 +22,3 @@ import { dirname, isAbsolute, join } from 'node:path'; | ||
// Static files | ||
app.use(sirv(join(__dirname, '../public'))); | ||
app.use(sirv(join(__dirname, '../public'), { dev: !isProduction })); | ||
options.static | ||
@@ -25,0 +25,0 @@ ?.map((path) => (isAbsolute(path) ? path : join(process.cwd(), path))) |
#!/usr/bin/env node | ||
import { readFileSync } from 'node:fs'; | ||
import { existsSync, readFileSync } from 'node:fs'; | ||
import { extname, join } from 'node:path'; | ||
import { parseArgs } from 'node:util'; | ||
import { watch } from 'chokidar'; | ||
import JSON5 from 'json5'; | ||
import { Low } from 'lowdb'; | ||
import { JSONFile } from 'lowdb/node'; | ||
import { DataFile, JSONFile } from 'lowdb/node'; | ||
import { createApp } from './app.js'; | ||
import { JSON5File } from './JSON5File.js'; | ||
import { Observer } from './Observer.js'; | ||
@@ -38,3 +38,3 @@ // Parse args | ||
if (values.help || positionals.length === 0) { | ||
console.log(`Usage: json-server [options] [file] | ||
console.log(`Usage: json-server [options] <file> | ||
Options: | ||
@@ -53,9 +53,21 @@ -p, --port <port> Port (default: 3000) | ||
// App args and options | ||
const file = positionals[0] ?? 'db.json'; | ||
const file = positionals[0] ?? ''; | ||
const port = parseInt(values.port ?? process.env['PORT'] ?? '3000'); | ||
const host = values.host ?? process.env['HOST'] ?? 'localhost'; | ||
// Check file | ||
if (file === '') { | ||
console.log('No file specified'); | ||
process.exit(1); | ||
} | ||
if (!existsSync(file)) { | ||
console.log(`File ${file} not found`); | ||
process.exit(1); | ||
} | ||
// Set up database | ||
let adapter; | ||
if (extname(file) === '.json5') { | ||
adapter = new JSON5File(file); | ||
adapter = new DataFile(file, { | ||
parse: JSON5.parse, | ||
stringify: JSON5.stringify, | ||
}); | ||
} | ||
@@ -82,4 +94,4 @@ else { | ||
}; | ||
observer.onReadStart = () => console.log(`reloading ${file}...`); | ||
observer.onReadEnd = () => console.log('reloaded'); | ||
observer.onReadStart = () => console.log(`Reloading ${file}...`); | ||
observer.onReadEnd = () => console.log('Reloaded'); | ||
watch(file).on('change', () => { | ||
@@ -101,3 +113,3 @@ // Do no reload if the file is being written to by the app | ||
console.log(`Started on :${port}`); | ||
console.log(routes(db)); | ||
console.log(routes(db).join('\n')); | ||
}); |
{ | ||
"name": "json-server", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"description": "", | ||
@@ -14,3 +14,3 @@ "type": "module", | ||
"css": "tailwindcss -i ./views/input.css -o ./public/output.css", | ||
"watch-ts": "tsx watch src/bin.ts db.json", | ||
"watch-ts": "tsx watch src/bin.ts fixtures/db.json", | ||
"watch-css": "npm run css -- --watch", | ||
@@ -30,3 +30,3 @@ "dev": "concurrently npm:watch-*", | ||
"@tailwindcss/typography": "^0.5.10", | ||
"@types/node": "^20.10.0", | ||
"@types/node": "^20.10.5", | ||
"@typicode/eslint-config": "^1.2.0", | ||
@@ -36,7 +36,7 @@ "concurrently": "^8.2.2", | ||
"husky": "^8.0.3", | ||
"tailwindcss": "^3.3.5", | ||
"tailwindcss": "^3.4.0", | ||
"tempy": "^3.1.0", | ||
"tsx": "^4.5.0", | ||
"type-fest": "^4.8.2", | ||
"typescript": "^5.3.2" | ||
"tsx": "^4.7.0", | ||
"type-fest": "^4.8.3", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -47,8 +47,8 @@ "dependencies": { | ||
"dot-prop": "^8.0.2", | ||
"eta": "^3.1.1", | ||
"eta": "^3.2.0", | ||
"inflection": "^3.0.0", | ||
"json5": "^2.2.3", | ||
"lowdb": "^6.1.1", | ||
"lowdb": "^7.0.1", | ||
"milliparsec": "^2.3.0", | ||
"sirv": "^2.0.3", | ||
"sirv": "^2.0.4", | ||
"sort-on": "^6.0.0", | ||
@@ -55,0 +55,0 @@ "zod": "^3.22.4" |
@@ -5,14 +5,11 @@ # json-server | ||
## Usage | ||
## Install | ||
Install `json-server` | ||
```shell | ||
npm install json-server | ||
npm install json-server@alpha | ||
``` | ||
Create a `db.json` file or run `json-server db.json` to create one with some default resources | ||
## Usage | ||
> [!TIP] | ||
> You can also use [json5](https://json5.org/) format by creating a `db.json5` instead | ||
Create a `db.json` or `db.json5` file | ||
@@ -32,4 +29,11 @@ ```json | ||
Pass it to JSON Server CLI | ||
```shell | ||
json-server db.json | ||
``` | ||
Get a REST API | ||
```shell | ||
curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1 | ||
@@ -50,3 +54,4 @@ { | ||
POST /posts | ||
PUT /posts | ||
PUT /posts/:id | ||
PATCH /posts/:id | ||
DELETE /posts/:id | ||
@@ -53,0 +58,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1826
126
161353
17
+ Addedlowdb@7.0.1(transitive)
+ Addedsteno@4.0.2(transitive)
- Removedlowdb@6.1.1(transitive)
- Removedsteno@3.2.0(transitive)
Updatedeta@^3.2.0
Updatedlowdb@^7.0.1
Updatedsirv@^2.0.4