Star

Created With

linkCustom Server



You can create custom TyFON servers using tyfon-server package. It allows you to either create independent express apps for serving some functions or create express routers that can be mounted on already existing node applications.

In this post, we will create an independent node app and use tyfon-serve to mount some functions on it.


linkStep 1: Setup

Lets create a folder for our server code:

1link$mkdir my-tyfon-server

2link$cd my-tyfon-server

3link$npm init


Now lets install some dependencies. tyfon-server works with express only, and it is highly recommended to use it alongside cors and body-parser middlewares:

1link$npm i tyfon-conventions tyfon-server express cors body-parser

2link$npm i typescript ts-node ts-node-dev @types/express @types/cors @types/body-parser --save-dev


Lets configure our NPM scripts in package.json to easily run the server:

package.json
1link{

2link ...

3link "scripts": {

4link "start": "ts-node-dev .",

5link },

6link ...

7link}


Finally, lets configure TypeScript for our project in tsconfig.json:

tsconfig.json
1link{

2link "compilerOptions": {

3link "noImplicitAny": true,

4link "strictNullChecks": true,

5link "strictFunctionTypes": true,

6link "noImplicitThis": true,

7link "alwaysStrict": true,

8link "declaration": true,

9link "sourceMap": true,

10link "moduleResolution": "node",

11link "esModuleInterop": true,

12link "allowSyntheticDefaultImports": true,

13link "experimentalDecorators": true,

14link "emitDecoratorMetadata": true,

15link "lib": [

16link "es2017",

17link "dom"

18link ]

19link },

20link "include": [

21link "./*",

22link "./**/*"

23link ]

24link}


linkStep 2: Create the App

First, lets create the main node app in index.ts:

index.ts
1linkimport express from 'express';

2linkimport cors from 'cors';

3linkimport { json } from 'body-parser';

4link

5linkconst app = express();

6linkapp.use(cors());

7linkapp.use(json());

8linkapp.get('/ping', (_, res) => res.status(200).send('It Works!'));

9link

10linkapp.listen(4000);


👉 Run the code and check it on http://localhost:4000/ping

1link$npm start


linkStep 3: Create the Function(s)

Lets create a folder for our functions, named funcs, and put our remote function inside it in funcs/index.ts as follows:

funcs/index.ts
1linkexport async function getMessage(name: string) {

2link return `Servus ${name}!`;

3link}


linkStep 4: Mount the Function(s)

Lets import the functions in funcs folder and mount them on our node app:

index.ts
1linkimport express from 'express';

2linkimport cors from 'cors';

3linkimport { json } from 'body-parser';

4link

5link import { router } from 'tyfon-server'; // --> import the router factory

6link import { jsonReviver } from 'tyfon-conventions'; // --> this is for custom json parsing optimized for TyFON servers

7link import * as funcs from './funcs'; // --> import our functions

8link

9linkconst app = express();

10linkapp.use(cors());

11linkapp.use(json());

12linkapp.use(json({ reviver: jsonReviver })); // --> this is for custom json parsing optimized for TyFON servers

13linkapp.get('/ping', (_, res) => res.status(200).send('It Works!'));

14link

15link app.use('/funcs', router(funcs)); // --> mount the router for our functions on the server

16link

17linkapp.listen(4000);


👉 Check it out on http://localhost:4000/funcs/message?0=World


linkStep 5: SDK Metadata

While our remote function is served perfectly, our server does not provide any SDK metadata yet. This means that TyFON CLI would not be able to auto-generate TyFON SDKs for our server.

To add that ability, we need TyFON CLI installed:

1link$npm i -g tyfon


First, lets give our functions a name (so that the SDK will have a proper name). Create funcs/package.json like this:

funcs/package.json
1link{

2link "name": "my-funcs"

3link}


Now lets go inside funcs folder and build the SDK metadata:

1link$cd funcs

2link$tyfon b

3link$cd ..

TyFON CLI has built our SDK metadata in funcs/dist/__api.json:

funcs/dist/__api.json
1link{

2link "name": "my-funcs",

3link "version": "0.0.0",

4link "types": {

5link "index.d.ts": "declare function getMessage(name: string): Promise<string>;\n\nexport { getMessage };\n"

6link },

7link "funcs": [

8link "getMessage"

9link ]

10link}


Finally, we need to tell our TyFON router to serve this file as well:

index.ts
1linkimport express from 'express';

2linkimport cors from 'cors';

3linkimport { json } from 'body-parser';

4link

5linkimport { router } from 'tyfon-server';

6linkimport { jsonReviver } from 'tyfon-conventions';

7linkimport * as funcs from './funcs';

8link

9linkconst app = express();

10linkapp.use(cors());

11linkapp.use(json({ reviver: jsonReviver }));

12linkapp.get('/ping', (_, res) => res.status(200).send('It Works!'));

13link

14link app.use('/funcs', router(funcs));

15link app.use('/funcs', router(funcs, require('./funcs/dist/__api')));

16link

17linkapp.listen(4000);

👉 Check it out on http://localhost:4000/funcs/__api


linkStep 6: Test the SDK

Lets create another project for testing out our SDK (keep the server running):

1link$mkdir my-tyfon-client

2link$cd my-tyfon-client

3link$npm i -g ts-node # if you don't have ts-node

4link$npm init


Install the SDK:

1link$tyfon i localhost:4000/funcs


Create index.ts:

index.ts
1linkimport { getMessage } from '@api/my-funcs';

2link

3linkgetMessage('Jack').then(console.log);


👉 Lets run it:

1link$ts-node .

Servus Jack!



Custom ServerStep 1: SetupStep 2: Create the AppStep 3: Create the Function(s)Step 4: Mount the Function(s)Step 5: SDK MetadataStep 6: Test the SDK

Home Conventions Error Handling

CLI Referencechevron_right
Advanced Usagechevron_right