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.
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.