0

I work with a starter Node and Express web server that is based on Webpack, using babel-loader and ts-loader.

Next, some parts of the code:

webpack-config.js:

const path = require("path");
const nodeExternals = require("webpack-node-externals");
const Dotenv = require("dotenv-webpack");
require("dotenv/config");

const OUTPUT_FOLDER = process.env.OUTPUT_FOLDER;

module.exports = {
  mode: "development",
  watch: true,
  node: {
    __dirname: false,
  },
  externalsPresets: { node: true },
  entry: "./index.ts",
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, OUTPUT_FOLDER),
  },
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: 'ts-loader',
      },
    ],
  },
  plugins: [new Dotenv()],
};

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "output",
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "allowJs": true,
    "moduleResolution": "node"
  },
  "include": ["src/**/*.ts", "test/**/*.ts"],
  "exclude": ["node_modules"]
}

index.ts:

import * as express from 'express';    
const app = express();    
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post("/", (req, res) => {
  console.log(req.body);
  res.json("OK");
});

app.listen(5000, () => console.log("Server started at 5000..."));

When executing POST request to the route http://localhost:5000, it activates the route in index.ts file, where is placed the command console.log(req.body); In the result, i see in the terminal the empty object, - {}.

The request is made by Postman program, as it seen in the screenshot:

enter image description here

It is identical to such a request:

axios.post("http://localhost:5000/", {
  _id: "12345",
  name: "Sim-sim open",
});

Thanks for attention!

Kiten
  • 985
  • 2
  • 17
  • 35
  • 1
    try sending the same request with raw JSON by selecting from rightmost list in the postman screenshot – cmgchess Oct 23 '21 at 17:30

1 Answers1

0

I suggest you check this question here, there is a nice discussion there and you will probably find something useful.

Make sure that you have Content-Type: application/json set in headers.