Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs not logging in terminal #1581

Open
neldivad opened this issue Jul 23, 2024 · 5 comments · May be fixed by #1625
Open

Logs not logging in terminal #1581

neldivad opened this issue Jul 23, 2024 · 5 comments · May be fixed by #1625

Comments

@neldivad
Copy link

npx json-server --watch data/db.json --port 3500
$ npx json-server --watch data/db.json --port 3500
--watch/-w can be omitted, JSON Server 1+ watches for file changes by default
JSON Server started on PORT :3500
Press CTRL-C to stop
Watching data/db.json...

(˶ᵔ ᵕ ᵔ˶)

Index:
http://localhost:3500/

Static files:
Serving ./public directory if it exists

Endpoints:
http://localhost:3500/bruh
http://localhost:3500/items

Expect printed logs when URL is accessed or when update request is made, but terminal is silent.

Did not install npm -i json-server, using json-server out of box via npx.

@Shub3am
Copy link

Shub3am commented Jul 25, 2024

just tested, facing same.

in fact, nothing is being printed on any flags whatsoever

@Nivedhatest
Copy link

Even i am facing the same issue thereby no log are getting captured.
Screenshot 2024-07-31 191116

@Kushagra-Gupta-2607
Copy link

Can i pick this up this issue?

@bbelbuken
Copy link

Anyone find any solution to this ?

@rkristelijn rkristelijn linked a pull request Jan 3, 2025 that will close this issue
@rkristelijn
Copy link

rkristelijn commented Jan 3, 2025

awaiting #1625 you can preview by adding this to your package.json:

"scripts": {
    "server": "node node_modules/json-server/lib/bin.js db.json --port 3000 --middleware=logger.mjs",
    "postinstall": "cd node_modules && git clone -b feature/add-middleware https://github.com/rkristelijn/json-server.git"
  }

and create a logger.mjs file:

// logger.mjs
import chalk from 'chalk';

export default (req, _res, next) => {
 const currentDate = new Date().toISOString();
 console.log(chalk.green(req.method), chalk.yellow(req.url), chalk.blue(`${currentDate}`));

 // Check if the request body is already parsed
 if (req.body && Object.keys(req.body).length > 0) {
   console.log(chalk.magenta('Body:'), req.body);
 } else {
   // Manually parse the request body if not already parsed
   let body = '';
   req.on('data', (chunk) => {
     body += chunk.toString();
   });
   req.on('end', () => {
     if (body) {
       try {
         const parsedBody = JSON.parse(body);
         console.log(chalk.magenta('Body:'), parsedBody);
       } catch (error) {
         console.log(chalk.red('Failed to parse body'), error);
       }
     }
     next();
   });
   return;
 }

 next();
};

it will output:

> node node_modules/json-server/lib/bin.js db.json --port 3000 --middleware=src/utils/logger.mjs

Loading middleware from src/utils/logger.mjs
app.ts: Using middleware [Function: default]
JSON Server started on PORT :3000
Press CTRL-C to stop
Watching db.json...

(˶ᵔ ᵕ ᵔ˶)

Index:
http://localhost:3000/

Static files:
Serving ./public directory if it exists

Endpoints:
http://localhost:3000/users
http://localhost:3000/users_columns

[2025-01-03T08:35:43.824Z] POST /posts
Body: { title: 'foo', body: 'bar', userId: 1 }
[2025-01-03T08:35:46.331Z] PATCH /posts/1
Body: { title: 'foo', body: 'bar', userId: 1 }
[2025-01-03T08:35:47.579Z] GET /posts

for these http calls

// test.http
// use https://marketplace.visualstudio.com/items?itemName=humao.rest-client to call
###
GET http://localhost:3000/posts

###
POST http://localhost:3000/posts
Content-Type: application/json

{
  "title": "foo",
  "body": "bar",
  "userId": 1
}

###
PATCH http://localhost:3000/posts/1
Content-Type: application/json

{
  "title": "foo",
  "body": "bar",
  "userId": 1
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants