It's been long time since you asked but FWIW here we go for anyone who inherited legacy Express app like I just did.
You can use your legacy Express application as part of NestJS using ExpressAdapter
. You just create (or import) the express()
app with all it's routes and then start Nest. The routes from legacy should be there so over time you just add routes to the Nest controllers/services modules and delete them from the legacy app.
import express, {json, urlencoded} from "express";
// imports legacy API; the file exports `module.exports = router;`
// where router is `const router = express.Router();`
import api from './legacy/routes/api.cjs';
config();
const expressApp = express();
expressApp.use(json());
expressApp.use(urlencoded({extended: false}));
expressApp.use('/api', api);
async function bootstrap() {
const adapter = new ExpressAdapter(expressApp);
const app = await NestFactory.create(AppModule, adapter);
await app.listen(3000);
}
bootstrap();
You should NOT start the server in the legacy app as Nest will do that for you. Comment out something like this that might be there in legacy:
const server = http.createServer(app);
server.listen(port, () => logger.logger.info(`API running on localhost:${port}`));
It seems to work for me so far.