Posts

Showing posts with the label express

Nodejs, express routes as es6 classes

Nodejs, express routes as es6 classes I want to clean up my project a bit and now i try to use es6 classes for my routes. My problem is that this is always undefined. var express = require('express'); var app = express(); class Routes { constructor(){ this.foo = 10 } Root(req, res, next){ res.json({foo: this.foo}); // TypeError: Cannot read property 'foo' of undefined } } var routes = new Routes(); app.get('/', routes.Root); app.listen(8080); 4 Answers 4 try to use the code to pin this : this app.get('/', routes.Root.bind(routes)); You can get out of the boilerplate using underscore bindAll function. For example: var _ = require('underscore'); // .. var routes = new Routes(); _.bindAll(routes) app.get('/', routes.Root); I also found that es7 allows you to write the code in a more elegant way: class Routes { construc...

single page application using nodejs and angularjs [on hold]

single page application using nodejs and angularjs [on hold] I am using postman to get and post data from API which I created and it is working properly using postman but I am unable to reflect those data in command prompt using command db.sports.find().Is there any other way to verify if data which I enter from postman by get and post method is stored in my database or not. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question. Hi, welcome to Stack Overflow! Please post more details about your implementation and if you are unable to provide code, please post screenshots so we can better understand what it is that you want us to help you with. Thanks! – SalmonKi...

Socket.io namespace stops working after 10 connections

Socket.io namespace stops working after 10 connections I'm using node.js, ejs, express, mysql and socket.io. My server looks like this (index.js): module.exports = function(io) { var express = require('express'); var router = express.Router(); var mysql = require('mysql'); var pool = mysql.createPool({ connectionLimit: 10, host: 'localhost', user: 'root', password: '', database: 'chat' }); router.get('/chat/:channel_name', (req, res, next) => { var channel_name = req.params.channel_name; // get the countries from the database so we make sure the users are connecting to the correct channel to chat, else redirect to /chat pool.getConnection((err, connection) => { if(err) return console.log(err); connection.query('SELECT * FROM channel WHERE channel_name = ?', channel_name, (err, rows) => { if(err) return console.log(err) if(!rows.length){ ...

Socket.io and Express not sending data

Socket.io and Express not sending data I'm trying to use socket.io to connect to this websocket api: https://www.cryptocompare.com/api/#-api-web-socket- (wss://streamer.cryptocompare.com) I guess im not really understanding socket.io very much. I created a blank html document: <!doctype html> <html lang="en"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.slim.js"></script> </head> <body> <div id="data-show"> </div> <button id="connect-sock">Connect</button> <button id="disconnect-sock">DISConnect</button> </body> <script src="index.js"></script> </html> index.js: var socket = io('wss://streamer.cryptocompare.com') console.log('connected') var btn = document.g...