Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is createServer() considered not a function?

I installed express globally and npm installed my express app, yet neither the intellisence or the app is working ( I am using visual studio code on mac OS Yosemite).

here is a sample code:

/// <reference path="typings/node/node.d.ts" />
/// <reference path="typings/express/express.d.ts" />

var express = require('express');
var app = express.createServer();
app.get('/', function (req, res) {
    res.send('hi');
})

app.listen(8000);

and here are the errors I am getting:

Abeds-MacBook-Pro:myNode AZ$ node twitter.js 
/Users/AZ/Desktop/myNode/twitter.js:5
var app = express.createServer();
                  ^

TypeError: express.createServer is not a function
    at Object.<anonymous> (/Users/AZ/Desktop/myNode/twitter.js:5:19)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:429:10)
    at startup (node.js:139:18)
    at node.js:999:3

I researched a bit and found that createServer() has been deprecated. I read that I need to change the version somewhere in my application.

Here is my application folder

Note: I did another application using purely Node.js, and createServer() did work without any error along with the intellisence.

EDIT: in my other application I used require('net') instead.

I modified my code to the following:

var express = require('express')
  , http = require('http');

    var app = express(); 
    var server = http.createServer(app);
    console.log('Listening on port 8000')
    app.get('/', function (req, res) {
        res.send('hi');
    })

    app.listen(8000)

The problem I have now is that res.send('hi'); has not been reached, aka, I cannot send to the client.

EDIT 2:

I tried the following code provided in one of the answers:

const express = require('express');
const http = require('http');

const app = express();
const server = http.createServer(app).listen(8080, function(err) {
  if (err) {
    console.log(err);
  } else {
    const host = server.address().address;
    const port = server.address().port;
    console.log(`Server listening on ${host}:${port}`);
  }
});
app.get('/', function (req, res) {
    res.send('hi');
})

res.send('hi'); still does not work, neither does it provide an error.

like image 251
abedzantout Avatar asked Jan 16 '16 17:01

abedzantout


People also ask

What is createServer ()?

createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.

How many arguments are passed to function () which is passed as an argument to createServer () function?

createServer() passes two arguments to the callback when you call it. The first is the http request object, the second is the http response object.

What is a server in node JS?

Node. js is an open source server environment. Node. js uses JavaScript on the server. The task of a web server is to open a file on the server and return the content to the client.

Which syntax is true to create an instance of HTTP module?

Syntax: var http = require('http'); We can create a HTTP server with the help of http. createServer() method.


1 Answers

createServer is a function of http so it should be:

const express = require('express');
const http = require('http');

const app = express();
app.get('/', function (req, res) {
  res.send('hi');
});

const server = http.createServer(app).listen(8080, function(err) {
  if (err) {
    console.log(err);
  } else {
    const host = server.address().address;
    const port = server.address().port;
    console.log(`Server listening on ${host}:${port}`);
  }
});

P.S. Installing express globally is a bad idea

like image 197
krl Avatar answered Oct 08 '22 09:10

krl