how to start json server in particular port

Starting a JSON Server on a Particular Port

If you're working on a project that requires a JSON server, you may need to start it on a particular port. Here's how you can do it:

Using the Command Line

If you're comfortable using the command line, you can start a JSON server on a particular port by running the following command:

json-server --watch db.json --port 3000

In this command, "json-server" is the name of the package that you'll need to have installed. "db.json" is the name of the file that contains your data, and "3000" is the port number you want to use.

Using JavaScript

If you're working with JavaScript, you can start a JSON server on a particular port by using the following code:

const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()

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

In this code, you'll need to have the "json-server" package installed. "db.json" is the name of the file that contains your data, and "3000" is the port number you want to use.

Starting a JSON server on a particular port is an essential part of working with JSON data. Whether you prefer to use the command line or JavaScript, there are multiple ways to get started.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe