GithubHelp home page GithubHelp logo

kitloong / json-server-vercel Goto Github PK

View Code? Open in Web Editor NEW
241.0 2.0 492.0 52 KB

Deploy JSON Server to Vercel

Home Page: https://json-server-in.vercel.app

License: MIT License

JavaScript 100.00%
deploy json-server mock-server restful-api vercel

json-server-vercel's Introduction

Deploy JSON Server to Vercel

A template to deploy JSON Server to Vercel, allow you to run fake REST API online!

Demo from this repository:

  1. https://json-server-in.vercel.app
  2. https://json-server-in.vercel.app/api/posts

Powered by Vercel

How to use

  1. Click "Use this template" or clone this repository.
  2. Update or use the default db.json in the repository.
  3. Sign Up or login into Vercel.
  4. From the Vercel dashboard, click "+ New Project" then "Import" your repository.
  5. In the "Configure Project" screen, leave everything default and click "Deploy".
  6. Wait until deployment is done, and your own JSON server is ready to serve!

Default db.json

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

Enable write operations

By default, only GET operation is allowed, thanks to the contribution by @VicAv99 at #6, we can now enable write operations as well.

You can find the example code in api/server.js.

Reference

  1. https://github.com/typicode/json-server
  2. https://vercel.com
  3. https://shadowsmith.com/how-to-deploy-an-express-api-to-vercel

json-server-vercel's People

Contributors

kitloong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-server-vercel's Issues

Data persistent for life time in post, put or delete req

is the updated data saved in the db.json JSON file for life time like if I can't delete data? I found so many blogs that don't save data for life time . it's automatically deleted after some time. So, there is anyother way to do that?

Bug

Hello,

In module usage everything is working perfect, but /register - does not register new users, it says - you must add "users" (but "users" already exist). Paradox: at the same time /register knows that your email was registered, by checking it in "users" and it sends message : "email already exist".

*User registered earlier localy.

const jsonServer = require("json-server");
const auth = require("json-server-auth");

const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();

server.db = router.db;

// Add this before server.use(router)
server.use(middlewares);
server.use(auth);

server.use(router);
server.listen(3000, () => {
  console.log("JSON Server is running");
});

// Export the Server API
module.exports = server;

Getting 500 error

When i try to post some in my db.json, i'm getting a 500 error on http request. The post is did but the response in code get a error and the code stops working. If you test the endpoint in postman, you will receive the following warning:

Error: EROFS: read-only file system, open 'db.json'
at Object.openSync (node:fs:600:3)
at writeFileSync (node:fs:2221:35)
at FileSync.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/adapters/FileSync.js:53:14)
at db.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/lib/main.js:37:21)
at /var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/write.js:5:8
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)
at create (/var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/plural.js:246:5)
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)

Can you help me, please?

EROFS: read-only file system, open 'db.json'

Try to send a post method

Error: EROFS: read-only file system, open 'db.json'
at Object.openSync (node:fs:590:3)
at writeFileSync (node:fs:2202:35)
at FileSync.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/adapters/FileSync.js:53:14)
at LodashWrapper.db.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/lib/main.js:37:21)
at /var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/write.js:5:8
at Layer.handle [as handle_request]
(/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)
at create (/var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/plural.js:222:5)
at Layer.handle [as handle_request]
(/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)

POST & DELETE request 500

Try to make post request but fail:

Error: EROFS: read-only file system, open './db.json'
at Object.openSync (node:fs:601:3)
at writeFileSync (node:fs:2249:35)
at FileSync.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/adapters/FileSync.js:53:14)
at db.write (/var/task/node_modules/.pnpm/[email protected]/node_modules/lowdb/lib/main.js:37:21)
at /var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/write.js:5:8
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)
at create (/var/task/node_modules/.pnpm/[email protected]/node_modules/json-server/lib/server/router/plural.js:246:5)
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/route.js:144:13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.