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