I have a file that creates an express server. Let’s call it foo_serever.coffee
# foo_server.coffee
module.exports = express.createServer()
And now I want to require that and send it requests directly in javascript. (Note, sendRequest is not a real method I just want one like it)
app = require './foo_server'
app.sendRequest 'get', '/some/url.html', (status, body, headers) ->
console.log 'yay' if status is 200
Is there a way to do this? Or do I need to spawn it as another process and send it requests over http which seems like much more of a pain.
Short answer: No. Express only works as a real HTTP server, not a simulated one. Use a library like request for testing. You don’t have to spawn a separate process to send HTTP requests.