-1

this is my code:

const request = require('request');
const app = require('express');
app.get('/', function (req, res) {
res.send('hello world');
});

app.listen(3000);

but for some reason i kepp getting this error : "app.get is not a function" I made sure that express is installed but it keeps throwing this error

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Othman9095
  • 47
  • 6
  • 2
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+express+get+is+not+a+function) of [TypeError: app.get is not a function](/q/40828932/4642212). Clearly documented in the [documentation](//expressjs.com/en/4x/api.html). – Sebastian Simon Sep 07 '21 at 11:06
  • 1
    require('express')() will work – Naren Sep 07 '21 at 11:07

1 Answers1

1

You must import express and call it like this:

const express = require('express')
const app = express() // ->  (because it is function)
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

or,

const request = require('request');
const app = require('express')(); // -> immediate call
app.get('/', function (req, res) {
res.send('hello world');
});

app.listen(3000);
Batuhan
  • 1,521
  • 12
  • 29