mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 13:14:12 +08:00
Going back to express
This commit is contained in:
10
package.json
10
package.json
@ -8,18 +8,14 @@
|
|||||||
"test": "test"
|
"test": "test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "~2.0.0",
|
|
||||||
"cors": "~2.1.0",
|
"cors": "~2.1.0",
|
||||||
"express": "~3.4.4",
|
"express": "^3.4.8",
|
||||||
"logan": "~0.0.2",
|
|
||||||
"superagent": "~0.15.7",
|
"superagent": "~0.15.7",
|
||||||
"underscore": "~1.5.2",
|
"underscore": "~1.5.2",
|
||||||
"underscore.inflections": "~0.2.1",
|
"underscore.inflections": "~0.2.1",
|
||||||
"moment": "~2.4.0",
|
"low": "^0.5.0",
|
||||||
"low": "^0.4.2",
|
|
||||||
"restify": "^2.6.3",
|
|
||||||
"yargs": "^1.2.1",
|
"yargs": "^1.2.1",
|
||||||
"ecstatic": "^0.4.13"
|
"chalk": "^0.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"supertest": "~0.8.1",
|
"supertest": "~0.8.1",
|
||||||
|
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 318 B |
Binary file not shown.
Before Width: | Height: | Size: 2.7 KiB |
@ -1,28 +1,37 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>JSON Server</title>
|
<title>JSON Server</title>
|
||||||
<link href="//netdna.bootstrapcdn.com/bootswatch/3.0.2/journal/bootstrap.min.css" rel="stylesheet">
|
<link href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/flatly/bootstrap.min.css" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="stylesheets/style.css">
|
<link rel="stylesheet" href="stylesheets/style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<a href="https://github.com/typicode/json-server" class="logo">
|
<p>
|
||||||
<img src="images/logo.png">
|
<a href="https://github.com/typicode/json-server" class="logo">
|
||||||
</a>
|
<img src="images/json.png">
|
||||||
|
</a>
|
||||||
<p class="alert alert-info">
|
|
||||||
Congrats! You're successfully running JSON Server.
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h2>Resources</h2>
|
<p>
|
||||||
|
<em>
|
||||||
|
Congrats! You're successfully running JSON Server.
|
||||||
|
</em>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h4>Routes</h4>
|
||||||
|
<p>
|
||||||
|
Here are the resources that JSON Server has loaded:
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ul id="resources">loading, please wait...</ul>
|
<ul id="resources">loading, please wait...</ul>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
To view database current state:
|
You can view database current state at any time:
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<a href="db">db</a>
|
<a href="db">db</a>
|
||||||
@ -30,26 +39,19 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h2>Requests</h2>
|
|
||||||
<p>
|
<p>
|
||||||
Resources can be accessed in various ways.
|
You can use GET, POST, PUT... and access your resources from anywhere
|
||||||
</p>
|
using CORS and JSONP.
|
||||||
<p>
|
|
||||||
JSON Server supports:
|
|
||||||
<ul>
|
|
||||||
<li>GET, POST, PUT, PATCH, DESTROY and OPTIONS verbs.</li>
|
|
||||||
<li>JSONP or CORS cross domain requests.</li>
|
|
||||||
</ul>
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h2>Documentation</h2>
|
<h4>Documentation</h4>
|
||||||
<p>
|
<p>
|
||||||
View
|
View
|
||||||
<a href="http://github.com/typicode/jsonserver">README</a>
|
<a href="http://github.com/typicode/jsonserver">README</a>
|
||||||
on GitHub.
|
on GitHub.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h2>Issues</h2>
|
<h4>Issues</h4>
|
||||||
<p>Please go
|
<p>Please go
|
||||||
<a href="https://github.com/typicode/jsonplaceholder/issues">here</a>.
|
<a href="https://github.com/typicode/jsonplaceholder/issues">here</a>.
|
||||||
</p>
|
</p>
|
||||||
|
@ -1,43 +1,12 @@
|
|||||||
body {
|
img {
|
||||||
padding: 50px;
|
padding-top: 50px;
|
||||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
padding-bottom: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 {
|
li {
|
||||||
font-weight: lighter;
|
list-style-type: square;
|
||||||
}
|
}
|
||||||
|
|
||||||
a {
|
h4 {
|
||||||
/*color: #00B7FF;*/
|
padding-top: 20px;
|
||||||
color: #3D72D1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hr {
|
|
||||||
border: 1px #EEE solid;
|
|
||||||
}
|
|
||||||
|
|
||||||
a.logo {
|
|
||||||
display: block;
|
|
||||||
text-align: center;
|
|
||||||
margin-bottom: 50px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.alert-success {
|
|
||||||
font-size: 18px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*.logo {
|
|
||||||
background: #00B7FF;
|
|
||||||
color: white;
|
|
||||||
font-size: 48px;
|
|
||||||
width: 100px;
|
|
||||||
height: 100px;
|
|
||||||
text-align: center;
|
|
||||||
vertical-align: middle;
|
|
||||||
line-height: 90px;
|
|
||||||
border-radius: 100px;
|
|
||||||
transform:rotate(90deg);
|
|
||||||
-ms-transform:rotate(90deg);
|
|
||||||
-webkit-transform:rotate(90deg);
|
|
||||||
margin-bottom: 50px;
|
|
||||||
}*/
|
|
@ -1,40 +1,40 @@
|
|||||||
var yargv = require('yargs')
|
|
||||||
|
var chalk = require('chalk')
|
||||||
var request = require('request')
|
var request = require('request')
|
||||||
var low = require('low')
|
var low = require('low')
|
||||||
var server = require('./server')
|
var server = require('./server')
|
||||||
|
|
||||||
function show() {
|
function hello() {
|
||||||
for (var prop in low.db) {
|
console.log(
|
||||||
console.log(server.url + '/' + prop)
|
chalk.green('\n{^ ^} Heya!\n')
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function start(port) {
|
function start(port) {
|
||||||
server.listen(port, function() {
|
for (var prop in low.db) {
|
||||||
console.log('%s listening at %s', server.name, server.url);
|
console.log('http://localhost:' + port + '/' + chalk.green(prop))
|
||||||
})
|
}
|
||||||
|
|
||||||
|
server.listen(port)
|
||||||
}
|
}
|
||||||
|
|
||||||
function run() {
|
function run(argv) {
|
||||||
var argv = yargv.usage('Usage: $0 <source>')
|
hello();
|
||||||
.demand(1)
|
|
||||||
.default('port', 3000)
|
|
||||||
.argv
|
|
||||||
|
|
||||||
var source = argv._[0]
|
var source = argv._[0]
|
||||||
|
|
||||||
|
console.log('Loading database from ' + source + '\n')
|
||||||
|
|
||||||
if (/\.json$/.test(source)) {
|
if (/\.json$/.test(source)) {
|
||||||
var path = process.cwd() + '/' + source
|
var path = process.cwd() + '/' + source
|
||||||
low.path = path
|
low.path = path
|
||||||
low.db = require(path);
|
low.db = require(path);
|
||||||
show()
|
start(argv.port)
|
||||||
start()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/\.js$/.test(source)) {
|
if (/\.js$/.test(source)) {
|
||||||
var path = process.cwd() + '/' + source
|
var path = process.cwd() + '/' + source
|
||||||
low.db = require(path).run();
|
low.db = require(path).run();
|
||||||
show()
|
|
||||||
start(argv.port)
|
start(argv.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,10 +47,12 @@ function run() {
|
|||||||
low.db = JSON.parse(res.text)
|
low.db = JSON.parse(res.text)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
show()
|
start(argv.port)
|
||||||
start()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n'
|
||||||
|
)
|
||||||
process.stdin.resume()
|
process.stdin.resume()
|
||||||
process.stdin.setEncoding('utf8')
|
process.stdin.setEncoding('utf8')
|
||||||
|
|
||||||
|
@ -1,33 +1,31 @@
|
|||||||
var _ = require('underscore')
|
var express = require('express')
|
||||||
var low = require('low')
|
var http = require('http')
|
||||||
var ecstatic = require('ecstatic')
|
var path = require('path')
|
||||||
var restify = require('restify')
|
var _ = require('underscore')
|
||||||
var utils = require('./utils')
|
var low = require('low')
|
||||||
|
var utils = require('./utils')
|
||||||
|
|
||||||
low._.createId = utils.createId
|
low._.createId = utils.createId
|
||||||
|
|
||||||
var server = restify.createServer({
|
var server = express()
|
||||||
name: 'JSON Server'
|
|
||||||
})
|
|
||||||
|
|
||||||
server.use(restify.acceptParser(server.acceptable))
|
server.set('port', process.env.PORT || 3000)
|
||||||
server.use(restify.queryParser())
|
server.use(express.logger('dev'))
|
||||||
server.use(restify.bodyParser())
|
server.use(express.json())
|
||||||
server.use(restify.CORS())
|
server.use(express.urlencoded())
|
||||||
server.use(restify.jsonp())
|
server.use(express.methodOverride())
|
||||||
server.use(restify.gzipResponse())
|
server.use(express.static(path.join(__dirname, '../public')))
|
||||||
|
server.use(server.router)
|
||||||
|
|
||||||
|
if ('development' == server.get('env')) {
|
||||||
|
server.use(express.errorHandler());
|
||||||
|
}
|
||||||
|
|
||||||
routes = {}
|
routes = {}
|
||||||
|
|
||||||
server.get(/^\/$|.*(.css)/, restify.serveStatic({
|
|
||||||
'directory': __dirname + '/../public',
|
|
||||||
'default': 'index.html'
|
|
||||||
}))
|
|
||||||
|
|
||||||
// GET /db
|
// GET /db
|
||||||
routes.db = function(req, res, next) {
|
routes.db = function(req, res, next) {
|
||||||
res.send(low.db)
|
res.jsonp(low.db)
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /:resource?attr=&attr=
|
// GET /:resource?attr=&attr=
|
||||||
@ -46,8 +44,7 @@ routes.list = function(req, res, next) {
|
|||||||
query = low(req.params.resource).where(properties)
|
query = low(req.params.resource).where(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(query.value())
|
res.jsonp(query.value())
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /:parent/:parentId/:resource
|
// GET /:parent/:parentId/:resource
|
||||||
@ -63,8 +60,7 @@ routes.nestedList = function(req, res, next) {
|
|||||||
.where(properties)
|
.where(properties)
|
||||||
.value()
|
.value()
|
||||||
|
|
||||||
res.send(resource)
|
res.jsonp(resource)
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /:resource/:id
|
// GET /:resource/:id
|
||||||
@ -73,8 +69,7 @@ routes.show = function(req, res, next) {
|
|||||||
.get(+req.params.id)
|
.get(+req.params.id)
|
||||||
.value()
|
.value()
|
||||||
|
|
||||||
res.send(resource)
|
res.jsonp(resource)
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /:resource
|
// POST /:resource
|
||||||
@ -83,8 +78,7 @@ routes.create = function(req, res, next) {
|
|||||||
.insert(req.body)
|
.insert(req.body)
|
||||||
.value()
|
.value()
|
||||||
|
|
||||||
res.send(resource)
|
res.jsonp(resource)
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT /:resource/:id
|
// PUT /:resource/:id
|
||||||
@ -94,21 +88,15 @@ routes.update = function(req, res, next) {
|
|||||||
.update(+req.params.id, req.body)
|
.update(+req.params.id, req.body)
|
||||||
.value()
|
.value()
|
||||||
|
|
||||||
res.send(resource)
|
res.jsonp(resource)
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE /:resource/:id
|
// DELETE /:resource/:id
|
||||||
routes.destroy = function(req, res, next) {
|
routes.destroy = function(req, res, next) {
|
||||||
try {
|
low(req.params.resource).remove(+req.params.id)
|
||||||
low(req.params.resource).remove(+req.params.id)
|
utils.clean()
|
||||||
utils.clean()
|
|
||||||
|
|
||||||
res.send(204)
|
res.send(204)
|
||||||
next()
|
|
||||||
} catch(e) {
|
|
||||||
console.trace(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
server.get('/db', routes.db)
|
server.get('/db', routes.db)
|
||||||
|
Reference in New Issue
Block a user