Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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