Answer the question
In order to leave comments, you need to log in
Node.JS how best to organize the export of an asynchronous function from a module? (coffeescript)
Good time of the day!
I sat down to write a project generator on node.js, express, coffee, etc. There are a great many of them, but everywhere they don’t like something, either the routing is inconvenient, or the configs cannot be set in files, in general, everything is not right. I want to be able to initiate the launch of an express server with a callback in the root app.coffee. I thought for a long time how to describe it in Russian better - I didn’t think of it, I’ll try to explain with examples:
The following module is connected to app.coffee
engine = require './app/engine'
express = require 'express'
http = require 'http'
app = express()
###
# Много страшной настройки express
###
# Вот тут все коряво, надо как-то исправлять
module.exports =
app: app
start: (done) ->
http.createServer(app).listen app.get('port'), -> done true # WTF: неужели нет другого способа?
engine.start -> print "Awesome server listening on port " + engine.app.get('port')
Answer the question
In order to leave comments, you need to log in
Why http? And what exactly is "inelegance"?
I would improve so:
1) Removed http;
2) Removed a separate method for returning app ;
3) I would not pick out the configs (in this case, the port) from the app inside the module, but would store
engine.coffee separately there:
express = require 'express'
app = express()
port = process.env.PORT or 5000
exports = module.exports = (callback) ->
app.listen port, callback
engine = require './engine'
app = engine ->
console.log "listening on " + app.address().port
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question