# TypeError: server.on is not a function

**URL:** <https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449>\
**Category:** General\
**Created:** [28 June 2023 01:06 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449 "2023-06-28T01:06:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ax2382](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ax2382/32/81010_2.png) [@ax2382](https://discourse.nodered.org/u/ax2382)\
**Post date:** [28 June 2023 01:06 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449/1 "2023-06-28T01:06:29Z")

</div>

Hello, node-heads 🙂 I'm installing node-red in a nest.js app and I'm getting the following error when running/starting the server. I have no clue how to fix this issue and documentation for nest.js apps is quite scarce. Any help is highly appreciated...

```auto
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { json } from 'express';

async function bootstrap() {
	const app = await NestFactory.create(AppModule);
	app.enableCors();
	app.use(json({ limit: '50mb' }));

	const config = new DocumentBuilder()
		.setTitle('OAST')
		.setDescription('The OAST API description')
		.setVersion('0.1')
		.build();

	const document = SwaggerModule.createDocument(app, config);
	SwaggerModule.setup('api', app, document);

	/**
	 * Configure node-red
	 */
	const httpAdapter = app.getHttpAdapter();
	const RED = require('node-red');

	// Settings object for node-red
	const nodeRedSettings = {
		httpAdminRoot: '/red',
		httpNodeRoot: '/api',
		userDir: '/home/nol/.nodered/',
		functionGlobalContext: {}, // enables global context
	};

	RED.init(httpAdapter, nodeRedSettings);
	// Serve the editor UI from /red
	app.use(nodeRedSettings.httpAdminRoot, RED.httpAdmin);
	// Serve the http nodes UI from /api
	app.use(nodeRedSettings.httpNodeRoot, RED.httpNode);

	await app.listen(3001);

	// Start the runtime
	RED.start();
}
bootstrap();

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 June 2023 06:59 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449/2 "2023-06-28T06:59:39Z")

</div>

Node-RED expects the server object it is given to be an instance of `express`. I know very little about `nest.js`, but it does appear to be able to wrap express somehow. The question is whether you are doing that and are you getting ahold of an express instance when you call `getHttpAdapter`.

I also note you have `userDir` set to `/home/nol/.nodered/` .... Make sure you change that to something suitable for your setup, unless of course you also have a user called `nol` like I do...

---

<div class="post-metadata">

**Author:** ![ax2382](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ax2382/32/81010_2.png) [@ax2382](https://discourse.nodered.org/u/ax2382)\
**Post date:** [28 June 2023 13:45 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449/3 "2023-06-28T13:45:11Z")

</div>

Thank you. Yes, nest.js is an Express MVC wrapper... I had to google how to get a hold of the app instance, I'll dig deeper, maybe the `const httpAdapter = app.getHttpAdapter();` is not the Express server object, maybe it's wrapped and the **on** function isn't mapped... or something...

---

<div class="post-metadata">

**Author:** ![ax2382](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ax2382/32/81010_2.png) [@ax2382](https://discourse.nodered.org/u/ax2382)\
**Post date:** [28 June 2023 17:16 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449/4 "2023-06-28T17:16:17Z")

</div>

This worked, thank you.

```auto
const httpAdapter = app.getHttpAdapter();
const appInstance = httpAdapter.getInstance();

// Settings object for node-red
const nodeRedSettings = {
		functionGlobalContext: {}, // enables global context
		httpAdminRoot: '/red',
		httpNodeRoot: '/api',
		userDir: '/home/nol/.nodered/',
	};

RED.init(appInstance, nodeRedSettings);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 July 2023 17:16 UTC](https://discourse.nodered.org/t/typeerror-server-on-is-not-a-function/79449/5 "2023-07-12T17:16:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
