Skip to content

Instantly share code, notes, and snippets.

@d0ruk
Created November 27, 2018 11:33
Show Gist options
  • Save d0ruk/3e20f69f5e398e9b563ac0bae8af5a61 to your computer and use it in GitHub Desktop.
Save d0ruk/3e20f69f5e398e9b563ac0bae8af5a61 to your computer and use it in GitHub Desktop.
nodemon with express+mongoose
version: "3"
services:
mongo:
image: mvertes/alpine-mongo
hostname: mongo
ports:
- 27017:27017
const express = require("express");
const mongoose = require("mongoose");
const app = express();
app.use((req, res, next) => {
res.send("<h1>Hello from Express!</h1>");
});
async function run() {
console.time("run");
const server = await app.listen({ host: "0.0.0.0", port: 3001 });
console.log("express app on 3001");
console.log("address: %j", server.address());
console.timeEnd("run");
}
async function run2() {
console.time("run2");
await mongoose.connect("mongodb://localhost:27017/test");
console.log("connected to mongo");
const server = await app.listen({ host: "0.0.0.0", port: 3001 });
console.log("express app on 3001");
console.log("address: %j", server.address());
console.timeEnd("run2");
}
async function run3() {
console.time("run3");
await mongoose.connect("mongodb://localhost:27017/test");
console.log("connected to mongo");
const server = await app.listen({ host: "0.0.0.0", port: 3001 });
console.log("express app on 3001");
setImmediate(() => {
console.log("address: %j", server.address());
})
console.timeEnd("run3");
}
/* just app.listen() - instance method .address() works correctly */
run()
/*
db.connect & app.listen() - .address() is undefined within
the currect iteration of the loop
*/
// run2()
/*
db.connect & app.listen() & setImmediate
.address() work correctly
the nature of synchronicity between mongoose and express
might be causing problems
*/
// run3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment