1

This is my server.js file code . I am trying to push the JSON content in the user object , but i am getting following error. Please tell me where i am going wrong.

const express = require('express')
const app = express()
const bcrypt = require('bcrypt')
const bodyParser = require('body-parser')
app.use(express.json())
const users = []

app.get('/users', (req, res) => {
  JSON.stringify(users)
  res.json(users)
})

app.post('/users', (req, res) => {
  const user = {
    name: req.body.name,
    password: req.body.password
  }
  users.push(user)
  res.status(201).send()
})

app.listen(3000, console.log("server started"));

I used an extension in VS code called REST client.

GET http: //localhost:3000/users

#####

POST http: //localhost:3000/users
Content-Type: application/json

{
  "name": "Tanay",
  "password": "password"
}

When I'm firing POST request it shows the error - SyntaxError: Unexpected end of JSON input

at JSON.parse (<anonymous>)
at parse (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:224:16)
at done (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Deshan Koswatte
  • 138
  • 1
  • 12
Tan_R
  • 27
  • 8
  • What exactly is your issue? Could you provide more information? – Nicolai Schmid May 23 '20 at 15:16
  • When i am firing POST request i am expecting it to push the JSON content into the users object but instead of it i am getting the above error. Hope this helps you understand the issue. – Tan_R May 23 '20 at 15:21
  • seems it is working perfectly. https://2886795285-5000-elsy06.environments.katacoda.com/users – anees May 23 '20 at 15:42
  • which version of express are you using? `express.json()` is deprecated now so use `body-parser` – AZ_ May 23 '20 at 15:43
  • @AZ_ even after using body-parser i am getting the same error – Tan_R May 23 '20 at 16:06
  • @AneesIjaz its not working on my system – Tan_R May 23 '20 at 16:09
  • then the issue could be with rest-client use `postman` or a `curl` or first try `content-type` also rather `Content-Type` – AZ_ May 23 '20 at 16:10
  • @AZ_ changing it to content-type also does not solve the issue – Tan_R May 23 '20 at 16:17
  • The issue is in JSON parsing and the code you have posted does not include that. You have added only a small part of code and whatever you have added is working perfectly. Issue is in different line of your code and you are posting something else over here – Deekshith Hegde May 23 '20 at 17:51
  • @hdeekshith i have posted everything over here . How is this working fine for you. When i hit the send request it shows me the error – Tan_R May 23 '20 at 18:02
  • It is working fine and I don't see any problem as well. Does your GET request work fine? Also comment the require bodyparser line as it is not used and try the POST request – Deekshith Hegde May 23 '20 at 18:04
  • I tried using POSTMAN after firing the POST request when i fire the GET request it is returning an empty object . Are you getting the object after GET request? – Tan_R May 23 '20 at 18:15
  • Isn't the post request working fine in Rest client? – Deekshith Hegde May 23 '20 at 18:17
  • @hdeekshith after POST request only i am getting that error . The problem is with the parsing itself but i am not able to figure out what exactly the issue is – Tan_R May 23 '20 at 18:20

1 Answers1

1

Can be something wrong with the user variable. Can you check this:

const user={'name':req.body.name,'password':req.body.password}

Update

I tried out:

var data = [];
const user={'name':"Deshan",'password':"password"}
data.push(user);
console.log(data);

And the result was as follow:

[ { name: 'Deshan', password: 'password' } ]

So it maybe a problem with the request data.

Deshan Koswatte
  • 138
  • 1
  • 12
  • Thanks for the response.. but still showing the same error. – Tan_R May 23 '20 at 15:26
  • Then it must be something wrong with the request data. Can you console log and see the request data? – Deshan Koswatte May 23 '20 at 15:35
  • what you have done is perfectly fine but i want to assign name and password through req.body.name and req.body.password . Can you tell me why it is not working – Tan_R May 23 '20 at 16:10
  • Yeah. Looking into the exception deeply seems there is a problem in parsing the JSON. You could try an answer at https://stackoverflow.com/questions/4295782/how-to-process-post-data-in-node-js – Deshan Koswatte May 23 '20 at 16:36
  • Hey @DeshanKoswatte, the key-value store should be in string format in JSON right. I see single quotes. – Balaji.J.B May 23 '20 at 16:47