Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  33] [ 7]  / answers: 1 / hits: 21345  / 9 Years ago, sat, january 16, 2016, 12:00:00

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



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.


More From » node.js

 Answers
48

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


[#63698] Friday, January 15, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stefanicarolinat

Total Points: 145
Total Questions: 91
Total Answers: 93

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
stefanicarolinat questions
Mon, Nov 15, 21, 00:00, 3 Years ago
Fri, Apr 16, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;