json-server icon indicating copy to clipboard operation
json-server copied to clipboard

How to serve image files?

Open nzmedet opened this issue 11 years ago • 13 comments

Sample image link http://localhost:3000/assets/images/coffee/large/coffee.gif not working with json-server, it's not possible to server static assets? (script files, css styles, images) Or is there any way to plug sharp (image resizer node package) into json-server

nzmedet avatar Apr 06 '15 16:04 nzmedet

To serve static assets, simply create a ./public directory.

For example:

$ mkdir -p public/assets/images
$ echo "hello world" > public/index.html
$ json-server db.json

If you need more, you can use json-server as a module.

var jsonServer = require('json-server')

var object = {
  posts: [
    { id: 1, body: 'foo' }
  ]
}

var router = jsonServer.router(object) // Express router
var server = jsonServer.create()       // Express server

server.use(router)
server.listen(3000)

You can add middlewares or custom routes as you would do in an Express server. You can even embed json-server router in an existing Express server.

Does that help?

typicode avatar Apr 07 '15 10:04 typicode

Closing for the moment. Feel free to reopen if needed.

typicode avatar May 07 '15 16:05 typicode

Hi typicode, Would you please elaborate a little on the above solution .I am totally new to this. -Aby

abyabraham avatar Oct 15 '16 11:10 abyabraham

after you add images to your public/assets/images directory, you can get an image like http://localhost/assets/images/truck-4.png

bluemix avatar Jul 17 '17 12:07 bluemix

I struggled with this too, so I wanted to share what worked for me:

var path = require('path')
var express = require('express')
var jsonServer = require('json-server')
var demodata = require('./db.json')
var routes = require('./routes.json')

var router = jsonServer.router(demodata) // Express router
var server = jsonServer.create()       // Express server

server.use('/static', express.static(path.join(__dirname, 'public')))

// Avoid CORS issue
server.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

server.use(jsonServer.rewriter(routes))
server.use(router)

server.listen(3004)

All my images are under public/assets/images, and I'm also storing the routes and db data in separate files. Hope this helps somebody!

camilodelvasto avatar May 21 '18 15:05 camilodelvasto

@camilodelvasto Thanks, it helps me get the images from json server. And for anyone else who comes here, please be aware that the folder name must be public. Then load images like this: http://localhost:3000/assets/images/abc.png

buildAll avatar Jul 27 '18 04:07 buildAll

after I add images to my public/assets/images directory, and add a Custom route like:

{
    "/api/test":"/assets/images/test.png"
} 

server return NOT FOUND 404:

GET /api/test 404 1.770 ms - 2

but I can access by the full path: http://localhost:5000/assets/images/test.png -->> 200 OK

mguilhermetavares avatar Oct 08 '18 14:10 mguilhermetavares

@mguilhermetavares any idea how it will work with custom routes also? Facing same issue as you but not able to figure out solution.

vickyru avatar Feb 13 '19 08:02 vickyru

I have put my images in public/assets/images path still I am not able to get image in http://localhost:3000/assets/images/uthappizz.png

vijeth11 avatar Mar 02 '19 14:03 vijeth11

If you are using public/assets/images and all of your images are inside this folder for example, the uri will be http://localhost:3000/image.jpg not http://localhost:3000/public/assets/images/image.jpg

Gabrielfcs avatar Sep 05 '19 01:09 Gabrielfcs

Images are loading in the local json-server. But when I'm hosting the json server using git repo, images are not accessing. Can anyone tell me how to access the same images through git fake api repo.

pavan-code avatar Jun 05 '20 20:06 pavan-code

as of v 0.17.0, if the image files are placed in ./public/assets/images/ they can be accessed using http://localhost:3000/assets/images/{imagename}

ank1t avatar Dec 27 '21 18:12 ank1t

I totally agree with @ank1t @buildAll . I had an svg file called jerome.svg in my public/asset/images folder. When i wanted to get the file, i used http://localhost:3000/assets/images/jerome.svg

valentinesamuel avatar May 19 '22 10:05 valentinesamuel