node.js - How do you export a variable from app.js into another route in Node js -


I am using socket.io in my node project and want to know how to possibly export the IO variable

I have done this yet, but I get this error: object # & lt; Object & gt; App There is no way in JS:

  var express = require ('express'); Var path = requirement ('path'); Var requires favicon = ('service-favicon'); Var Loger = Required ('Morgan'); Var requires cookieParser = ('cookie-parser'); Var bodyParser = is required ('body-parser'); Var routes = requirement ('. / Route / index') (io); Var requires user = ('./routes / user'); Requires Var app = ('Express') (); Var requires server = ('http'). Server (app); Var io = Requirement ('socket.io') (server); Server.listen (4000); Required ('./roads / index') (Io); // View engine setup app.set ('views', path.join (__dirname, 'views')); App.set ('visual engine', 'z'); // app.ws (favicon (__dirname + '/ public / img / favicon.eco')); App.use (logger ('god')); App.use (bodyParser.json ()); App.use (bodyParser.urlencoded ({extended: true})); App.use (cookieParser ()); App Use (express.static (path.join (__derename, 'public')); App.use ('/', route); App.use ('/ user', user); /// hold 404 and further error handler app.use (function (req, res, next) {var err = new error ('not found'); err.status = 404; next (err);}); /// Error handler // Development error handler // Print will print if if (app .get ('env') === 'development') {app.use (function (error, request, ridge, next) {res. Position (err.status || 500); res.render ('error', {message: err.message, error: err, title: 'error'});}); } // Production Error Handler // No stacktrace user is not leaked to the app. Use (function (mistake, rick, race, next) {res.status (err.status || 500); res.render ('error', {message: Err.message, error: {}, title: 'error' });}); / * Io.on ('connection', function (socket) {socket.emit ('news', {hello: 'world'}); socket.on ('my other events', function (data) {console.log (Information);   }); }); * / Module.exports = App;  

In index.js

  var io = require ('../app'); Io.on ('connection', function (socket) {socket.emit ('news', {hello: 'world'}); socket.on ('My other events', function (data) {console.log );});});  

The reason why I want to export the variable is so that I do not have to define the IO like index.js I did in app.js, which is basically the server i Defines app variables!

I just have all your sockets in index.js root Oz (in addition to redefining the IO variable in the app, server and index.js)!

Hope I have explained myself

Thank you!

Your dependency is not set correctly, index.js are executed before starting iio

  requires different routes ('./routes / index'); / * Index.js is being executed at this point * Therefore, IO is undefined within index.js * /  

, instead, you

app.js

  requires the app = ('Express') (); Var requires server = ('http'). Server (app); Var io = Requirement ('socket.io') (server); Server.listen (4000); Module.exports = Io; Var routes = required ('. / Route / index'); Var requires user = ('./routes / user');  

index.js

  var io = requirement ('../ app'); Io.on ('connection', function (socket) {socket.emit ('news', {hello: 'world'}); socket.on ('my other events', function (data) {console.log );});});  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -