access base http server of sails.js - node.js

Hi is there a way to access the base http server context of sails? I want to use binaryJS in my app and In the gettig started guide they are talking about creating the server at your own, it you have an existing express app with the following line:
var server = http.createServer(app).listen(9000);
than I could use the following binaryJS command:
// Create a BinaryServer attached to our existing server
var binaryserver = new BinaryServer({server: server, path: '/binary-endpoint'});
thank you

In Sails v0.10.x, you can access the underlying Express server with:
sails.hooks.http.server
in v0.9.x, it's
sails.express.server

Related

Error connecting to socket.io server, changed code from websocket to socket.io

I'm currently using websocket library on both nodejs backend and JS client.
I'm trying to change to socket.io, I managed to write the code for both sides however I'm unable to connect to the server from my client.
On the console of my browser I see the error and realised that the address is being changed by the socketio library.
websocket: wss://domain.com/asset-ws/ ==> works fine
socketio: auto changes the url to wss://domain.com/socket.io/?EIO=4&transport=websocket
The error: WebSocket connection to 'wss://domain.com/socket.io/?IO=4&transport=websocket' failed.
Is it something to do with my nginx config?
Any help would be much appreciated.
Thank you
By default socket.io url adds the /socket.io path.
In my Nginx config I'm already using a custom path add, so I just specified a path on the io server config, like so:
const io = new Server(server, {
path: '/'
});
This way the default path would be overwritten.

Get Websocket URL from Node js

I am trying to build a simple WebSockets app with node.js as a server. In order to create a WebSocket connection I write:
const url = 'ws://localhost:8080'
const connection = new WebSocket(url)
But I do not want to hardcode the URL. Can I receive it from my server?
Available options:
Using of Webpack/Rollup/Snowpack - https://dev.to/sanfra1407/how-to-use-env-file-in-javascript-applications-with-webpack-18df:
You can define a some .env and define address of server
Use dotenv or something another library on backend side to receive variable
Use Webpack/Rollup/Snowpack on frontend side to build frontend with defined address of server
It will allow to change address of server in one place
SSR (server-side rendering) - https://medium.com/jspoint/a-beginners-guide-to-react-server-side-rendering-ssr-bf3853841d55
You can provide address of server from backend to frontend

How to run Node.js server in Ionic mobile app?

I am making an app using MEAN and ionic framework where nodejs is a middleware to connect to the database(mongoDb). I need to run the nodejs server using node server.js and the app using ionic serve. This is my server.js.
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
CohortController =require('./www/server/controller/CohortController');
mongoose.connect('mongodb://localhost:27017/persistent');
app.use(bodyParser());
app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);
app.listen(3000,function(){
console.log('Listening...');
})
Now this is my app.js. I use http://localhost:3000 to get the JSON.
app.controller('CohortController',['$scope','$resource',
function($scope,$resource){
var Cohort=$resource('http://localhost:3000/api/cohorts');
Cohort.query(function(results){
$scope.cohorts=results;
});
$scope.cohorts=[];
$scope.createCohort= function () {
var cohort=new Cohort();
cohort.name=$scope.CohortName;
cohort.id=$scope.CohortId;
cohort.$save(function(result){
$scope.cohorts.push(result);
$scope.CohortName='';
$scope.CohortId='';
});
}
}]);
How can I run the node server when I convert it into a mobile application? How the application will use the API?
You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it's public IP. So, you wouldn't use http://localhost:3000 to get the JSON, instead you would use something like http://123.456.789.123:3000.
But, usually, this is not the way you would do it (with the port 3000). What you would additionally do is put (for example) Nginx in front of your Node.js app (see an example here) in order to serve your api from the standard HTTP port (80).
So, basically, you can't actually "run Node.js server in Ionic app" - the way you do it is run the Node.js app separate from Ionic and expose its functionality via a standardized API (usually these days REST is what you would want to achieve) which you then "consume" via Ionic's (well, to be exact, it's actually Angular's) $resource module.
Hope this helps clear things up a bit.

Error binding socket on heroku , not sure about using express

this is my first node.js and socket.io application , i didn't use express ,I want to deploy the application on heroku do i need to use it ? i mean i just did npm install socket.io on localhost and in my server file i.e game.js i have io = require("socket.io") and socket = io.listen(Number(process.env.PORT)) only and in one of the files where from where i am sending the message i have socket = io.connect();
so please tell me if i need to use express and how show i modify my existing application ?
I have given the link to the source of application
( https://github.com/bitgeeky/herokutest )
Although the Application works fine on localhost by changing the port no , to some port no like (8000) but Heroku error log on doing "heroku open" is http://pastebin.com/MtB0z5vQ
I noticed that you haven't created a http server. I am assuming that you are creating a web application, since you are deploying to heroku. For that, you need to create a http server in nodejs.
Go through socket.io https://github.com/LearnBoost/socket.io
Also http://socket.io/#how-to-use
This should get you started
Note: You do not need express. But it will make your work easier in many ways. Depends on the type of application that you want to create.

Structure of Express application with Redis

I am unsure about where would be the best place to define the redis client in a Express application I am building. I am using skeleton as the framework.
It seems like the connection to redis should go in either boot.coffee or app.coffee, but then I can't easily get a reference to it in application_controller.coffee, which is where I need it.
If I put client = redis.createClient in application_controller.coffee, will that mean a new client is created on each request?
I would define the Redis client in app.coffee (after configuration, before the routes) and set the Redis client as a property on the App object:
app.client = redis.createClient
Then in application_controller.coffee you can access the Redis client by app.client.

Resources