My requires:
//app.js Socket IO Test
var app = require('express').createServer(),
redis = require('socket.io/node_modules/redis'),
io = require('socket.io').listen(app);
My error:
Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:
var express = require("express");
var app = express();
How do I modify my declarations to avoid this error? I understand this methodology is now deprecated for Express, just not sure what it needs to be changed to...
Thanks in advance!
Simply replace var app = require('express').createServer() with:
var express = require("express");
var app = express();
Related
I am working on a small application that is hosted via node.js and uses socket.io for quick communication. For now it was only in development so it was fine. However now i would like to switch from ws:// to wss:// but dont really know where to start. I tried to research online but there is not much on the topic.
index.js (server)
const path = require("path");
const express = require("express");
const app = express();
const cors = require("cors");
const config = require("./config");
const webRoute = require("./lib/routes/web");
const widgetsRoute = require("./lib/routes/widgets");
const errorsHandler = require("./lib/handlers/errors");
const corsHandler = require("./lib/handlers/cors");
app.use(cors(corsHandler));
app.use(express.static(path.join(__dirname, "/public")));
app.use("/", webRoute);
app.use("/widgets", widgetsRoute);
app.use(errorsHandler);
app.listen(4118, "0.0.0.0");
const io = require("socket.io").listen(
require("http")
.createServer()
.listen(4113, "0.0.0.0")
);
require("./lib/inits/socket")(io);
client:
this.socket = io("http://example.org:4113");
In the network tab i can see that socket.io is using ws:// in one of its calls.
How can i modify this so it uses wss instead?
Is express() function used in the second statement a global function?.
Where can I find its declaration?. I could not find it in my project folder.
var express = require('express');
var app = express();
var fs = require("fs");
Here is what you are doing:
// creating a variable named express and storing return value of require function
// require is a nodejs function, in this case it is called with parameter called express which loads express module
var express = require('express');
// Executing the function stored in express variable
// And storing the result into app variable
var app = express();
So, where does the express comes, you are declaring it in line 1. var express = require('express') is just a convention, you can use any valid variable name. Following would also work:
var expServer = require('express');
var app = expServer();
Express is a npm module and you need to import it in order to use it, jsut like other npm packages.
Where can I find its declaration?. I could not find it in my project folder.
Its declaration is in the node_modules directory and you don't have to do anything with it.
Here is the example for using express and creating a server from it.
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.listen(3000);
I have just started using node.js and I can build a simple app that responds to requests and has some basic routing using the express framework.
I am looking to create something using socket.io but I am slightly confused over the use of the 'http' module. I understand what http is but I don't seem to need it for the following to work:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.htm');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
I can serve a html page over http without requiring the http module explicitly with something such as:
var http = require('http');
If I am using express do I have any use for the http module?
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(1234);
However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
var express = require('express');
var app = express();
var socketio = require('socket.io');
// app.use/routes/etc...
var server = app.listen(3033);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
...
});
source
http://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen
no, you probably don't need it.
You can use something like:
var app = require('express').createServer();
var io = require('socket.io')(app);
//Your express and socket.io code goes here:
I'm using express in my app.js I set something like this
var express = require('express');
var app = express();
app.set('myVar', 'hello');
then in my controller I want to get the value. I do
var express = require('express');
var app = express();
console.log(app.get('myVar')) // undefineded
Any idea why?
Your controller creates a new, fresh instance of Express. If you want to be able to share variables, you need to pass the instance from app.js to your controller:
// app.js
var express = require('express');
var app = express();
app.set('myVar', 'hello');
require('./controller')(app);
// controller.js
module.exports = function(app) {
console.log(app.get('myVar'));
};
EDIT: judging by the comments, the issue isn't so much passing app around, but moving parts of the application to separate modules. A common setup to enable that would look like this:
// app.js
var express = require('express');
var app = express();
app.set('myVar', 'hello');
app.use('/api', require('./controller/auth'));
// controller/auth.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
console.log(req.app.get('myVar'));
return res.send('hello world');
});
module.exports = router;
In your example you are instantiating a second app that you then try to get the value from. You need to get from the exact same object:
var express = require('express');
var app = express();
app.set('foo', 'bar');
app.get('foo');
If you are new to express, you can use the cli generator to scaffold out an application that shows you a sane pattern how to use the same express instance throughout your whole application.
I have an application built on express. I have an app.js that starts the app, and a functions.js where I have stored a lot of the functionality of the app. I would like to be able to access the socket.io object to use in my functions.js. However, it seems that I cannot do this and I can't find any solution on the internet either.
app.js:
var app = express(); //var app = express.createServer();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
functions.js:
//something like io = require('./app').io
I want to be able to access the io object so I can emit messages to my client side javascript.
Use module.exports to make the io object available to other modules.
app.js:
var express = require('express');
var app = express();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
app.io = io;
module.exports = app;
require('./functions')
functions.js:
var io = require('./app').io;
io.on('connection', function(socket) {
console.log('a user connected');
);