简体   繁体   中英

Request Hangs in express.js

I have created a brand new express API application and for some reason when I run it (the command is node src/index.js ) both requests hang and there is never a response. I have been able to nail it down to the app.use(express.json) line as the culprit. When it is commented out the requests seem to work, but then I do not get access to request body.

const app = express();

app.use(express.json);

app.post("/api/user/register", (req, res) => {
    res.send("Ok");
});

app.listen(3000, () => {
    console.log("API is up and running!");
});

The contents of my package.json file is as follows:

  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

I am currently running node version 13.2.0. Any suggestions on what to try next? (I'm new to node so any help would be appreciated)

It's supposed to be app.use(express.json()); not app.use(express.json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM