I want to know is the format must use symmetry?
This is my json !!!
{
"foo": "Hello",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock.js"
}
Hi Flyweights,
you should have a top-level object that contains your end points which contain the final objects you would like to save, could be an array of objects or just one object
{
"files": [
{
"foo": "Hello",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock.js"
},
{
"foo": "Hello2",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock2.js"
}
]
}
then you could make a request like:
/files -> will return array of objects [
{
"foo": "Hello",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock.js"
},
{
"foo": "Hello2",
"nested": {
"a": {
"b": {
"c": "Mock2.js"
}
}
},
"absolutePath": "Hello Mock2.js"
}
]
/files/0 -> will return the first object{
"foo": "Hello",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock.js"
}
It's not working for serving GeoJson data, for the sample see:
````json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type":"Point",
"coordinates": [126.3832,5.9775,111.16]
},
"properties": {
}
},
{
"type": "Feature",
"geometry": {
"type":"Point",
"coordinates": [126.3832,5.9775,111.16]
},
"properties": {
}
}
]
}
````
@armpogart - I had the same issue you are having with type. Since type returns a string, we end up getting the Type of 'type' (string) error. It is the content of the request, not the key. Unfortunate, since some of my apis return just a string as the whole body.
@fillippeyton Nope, I'm pretty sure that my api returns correctly formatted application/json.
@armpogart I mean type in your GeoJson, not the content-type header:
{
"type": "FeatureCollection",
"features": [
...
}
@armpogart @fillippeyton I'm also serving GeoJSON and @Nilegfx solution works perfectly, just wrap your GeoJSON into another object, give it a descriptive name that'll be used as the endpoint.
If you're fetching from an existing API and changing the response is not an option you can always wrap the response before passing it along to be rendered (e.g. Leafleft, Mapbox, etc.)
/files/0-> will return the first object
This dosn't work for me using the example from @Nilegfx, I get {} instead of
{
"foo": "Hello",
"nested": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "Hello Mock.js"
}
@armpogart
`const jsonServer = require('json-server')
const fetch = require('node-fetch')
const fs = require('fs')
const server = jsonServer.create()
const router = jsonServer.router('./parseller.json')
const middlewares = jsonServer.defaults()
server.use(middlewares)
server.get('/start', (req, res) => {
res.jsonp('okey')
fetch('http://..../geoserver/ows?service=WFS&request=GetFeature&typename=....&outputFormat=application/json')
.then(res => res.json())
.then(json => {
let text = '{ "data":' + JSON.stringify(json) + '}'
fs.writeFile('parseller.json', text, (err) => {
if (err) throw err
console.log('Replaced!')
})
})
})
server.use(router)
server.listen(3000, () => {
console.log('JSON Server is running')
})`
http://localhost:3000/start
json file change
http://localhost:3000/data
all json data
should support return plain text
This dosn't work for me using the example from @Nilegfx, I get
{}instead of
Same with u
Most helpful comment
Hi Flyweights,
you should have a top-level object that contains your end points which contain the final objects you would like to save, could be an array of objects or just one object
then you could make a request like:
/files-> will return array of objects/files/0-> will return the first object