im running unit test using mocha/supertest/chai and I want to test the following code so my question how should I simulate the upgrade event?
const http = require('http');
const app = require('./app');
const server = http.createServer(app);
var webSocket = function (server) {
server.on('upgrade', function (req, socket, head) {
//I want to enter here...
Related
I am trying like this, but anyway see only cannot get /
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
app.use(express.static(path.join(__dirname+"/public")))
server.listen(5000);
I'm learning about Dependency Injection and trying to use it in a simple nodejs app. This application has to fetch some data with an external API. Using DI in that case was simple, I just added the dependency as a parameter of the function and it worked like a charm.
async function foo(URL,get_data){
var bar = await get_data(URL);
return bar;
}
When the application is running, get_data would be the function that does the real request and when unit testing my app it would be a dummy function.
How do I apply this same methodology for ExpressJS?
I want it to access the database when is using the application and dummy data when it is testing, but I can't figure out how because I'm not dealing with properties of functions anymore.
In this example:
// index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
var db = require('./model/db')
app.get('/users',(req,res) =>{
db.get_users(function(users){
res.send(users);
})
});
app.listen(PORT, () => console.log(`Server listening to port: ${PORT}`));
module.exports = app;
I need to use the real db when the server is running and a mock one when testing with supertest.
// index.text.js
const request = require('supertest');
const app = require('index');
describe("GET /user", function(){
it('should respond with a json', function(done){
request(app)
.get('/users')
.expect('Content-Type',/json/)
.expect(200,done);
});
});
I'm having trouble with being able to connect to my node.js server from an external domain. It works fine when running it locally using the http web server through node however when connecting externally, it loads the socket.io.js file just fine but when trying to use the socket it removes the port from the URL and cannot connect.
Instead of doing this in the network requests:
http://external-domain.com:3000/socket.io/?EIO=3&transport=polling&t=M06GOUU
it does this:
http://external-domain.com/socket.io/?EIO=3&transport=polling&t=M06GOUU
I'm not sure how to make it not remove the port from the connection. How do I go about fixing this?
SERVER
const path = require('path');
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const publicPath = path.join(__dirname, '../public');
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.use(express.static(publicPath));
server.listen(3000, () => {
console.log(`Server is up on port 3000`);
});
CLIENT SCRIPT TAG
<script src="http://external-domain.com:3000/socket.io/socket.io.js"></script>
CLIENT JS ON A DIFFERENT DOMAIN
var socket = io();
socket.connect('http://external-domain.com:3000');
socket.on('connect', function () {
console.log('Connected to server.');
});
Change from this:
var socket = io();
socket.connect('http://external-domain.com:3000');
to just this:
var socket = io("http://external-domain.com:3000");
And, you don't use the socket.connect() as you will already have requested the connection with the io("http://external-domain.com:3000"); call.
Explanation
The code:
var socket = io();
uses the page URL to connect to a socket.io server at that origin. That is not what you want (apparently).
If you wanted to use the .connect() method, it would be like this:
var socket = io.connect("http://external-domain.com:3000");
Note: var socket = io(url) is simply a shortcut for var socket = io.connect(url).
socket.connect() does not accept a URL as a parameter so you simply weren't using that correctly. It's just a synonym for socket.open().
Use io.connect("url")
var socket = io.connect("http://external-domain.com:3000", { rejectUnauthorized: false });
// { rejectUnauthorized: false } is an optional parameter.
Hope this works for you.
This code runs in the server. I am making a simple websocket on the server and it looks for connections made to it. However, IntelliJ does not recognize the on() method that has been called on io. I am using IntelliJ latest version and coding in Node.js
var http = require('http');
var express = require('express');
var socket = require('socket.io');
function onRequest(req,res)
{
console.log('User requested for page: ',req.url);
}
// create a middleware application
var app = express();
app.use(onRequest);
// serve static files
app.use(express.static('public'));
var server = http.createServer(app).listen(4000);
// setup the socket on the server
var io = socket(server);
io.on('connection',function(socket)
{
console.log('Socket id is: ',socket.id);
});
Try npm install #types/socket.io. It will add the necessary definition file.
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: