0

I have trying to use JSON type in my node js application but i am getting data in some data of scripted type.

my response }, data: '\x1F�\b\x00\x00\x00\x00\x00\x00\x00]PMo�0\f�-�ykӬ_ ��\n' + '(\x14�\x1AVi�K\x15�\x00�4�\x14\x01���RN\x13��z�{~���\taJ�Z����\x16\x00�\x10�>�5rç\x18�c���\x1D:�ɓ���y5[\x15�N��\V����Eq�SyD�\ff� ށ\x07�\x1C\x7F|Wiw�f�M��%����&[���↸��\t���J(�\x05ae�r��u���c�x���(;�\n' + '\x0B��\x17����3��-a6�u/A�\x115S�aĩ��p�8N\r���\x1D\x02�ə�qj��(��aF3a�em�A���\x12k\x03�A0�a\x04#\x1F\x00��T�\x17XwmO �5C��nV��ħ�&?ɐ�/�d4Ab���Y�!y�~�\x00�\x16�:\x7F\x01\x00\x00' }

i can't use JSON in application,even after using express.json in index.ts file

   require("dotenv").config();
import express, { Request, Response, Express } from "express";

import { createProxyMiddleware } from "http-proxy-middleware";
import sfHelper from "./v1/helper/sfhelper";
import getconfigdata from "./v1/helper/confighelper";
import router from "./router/index";
const bodyParser = require('body-parser');
var cors = require('cors')

const app: Express = express();

app.use("/api", router);

app.use(express.json());

app.use(cors())

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({
   extended: true
}));

app.listen(process.env.PORT, async () => {
  console.log("App Runnig in 4000");
});
7
  • try to use router middleware after bodyparser middleware. Commented Nov 27, 2022 at 6:04
  • I have tried it but it given same response Commented Nov 27, 2022 at 6:06
  • Can you show your whole index.ts file Commented Nov 27, 2022 at 6:07
  • i have updated the file can you please take a look Commented Nov 27, 2022 at 6:12
  • You don't need both express.json and bodyparser.json, they do the same thing. Whichever you use, it should be above the app.use("/api", router). If that doesn't fix things, can you log out the content-type of the request to see if it's actually JSON? Commented Nov 27, 2022 at 6:16

2 Answers 2

1

As suggested by @ZacAnger you should declare your middleware before the routes.
There is no need to use body-parser (which is also deprecated):

const app: Express = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(cors());

app.use('/api', router);

app.listen(process.env.PORT, async () => {
  console.log('App Runnig in 4000');
});
Sign up to request clarification or add additional context in comments.

Comments

0

I have used fetch instead of axios it fixed my problem,i am not sure use axios is giving this response

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.