0

Ok please tell me if I have got this correct.

1) When I do this…

const express = require(“express”)

I store a “Class” into the express variable.

2) Then when I do this…

express.jason()

Am I accessing the jason() function inside the express class ?

ooooohyesssss
  • 119
  • 1
  • 1
  • 7
  • 1
    I'd strongly recommend a basic Node tutorial. Also it's `.json`. And you seem to have already had this answered: https://stackoverflow.com/q/59764123/3001761. – jonrsharpe Jan 16 '20 at 21:43
  • Isn't this already covered in our conversation [here](https://stackoverflow.com/questions/59764123/when-i-require-express-i-need-to-know-if-its-an-object-or-function/59764437?noredirect=1#comment105700423_59764437) and my answer there? Just because I didn't respond in immediately (I was eating lunch), doesn't mean you should fire up a new question about something we're already engaged in discussing that's just a clarification of an answer I've already provided. – jfriend00 Jan 16 '20 at 22:42

1 Answers1

0

1) Yes, using require you store whatever the package exports. It may be the pure Class, or a Class instance or a Function or some other variable.

2) Well, when you call express.json() you are calling .json() function from inside express instance from some class-like structure, yes.

Disclaimer:

You are likely to use the return of express.json() the following way to set a new express REST api:

const express = require('express')
const app = express()

app.use(express.json())
app.post(
  '/test',
  (req, res) => res.json(req.body)
)

const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`)
})
ofundefined
  • 2,692
  • 2
  • 18
  • 35