Introduction
Installation
If you have installed the necessary prerequisites and have a working Node.js installation, then you can start using Nokron right away. This chapter will show you how to create a new project and install Nokron.
Setting up a Typescript Node.js project
To create a new TypeScript Node.js project, you can use the npm init
command in your terminal. This will guide you through the process of creating a new package.json file for your project. To create a new project with the name "my-nokron-project", run the following command in your terminal:
mkdir my-nokron-project && cd my-nokron-project
npm init -y
This will create a new directory called "my-nokron-project" and initialize a new package.json file in it. The -y
flag will skip the interactive questions and use the default values for all of them. If you want to customize your project, you can omit the -y
flag and answer the questions interactively.
Next, you need to install the TypeScript compiler and the Node.js types. You can do this by running the following command:
npm install --save-dev typescript @types/node
When you have installed the TypeScript compiler and the Node.js types, create a new file called tsconfig.json
in the root of your project. This file will contain the configuration for the TypeScript compiler. Copy the following configuration into the file:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"esModuleInterop": true,
"strict": true,
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/*"]
},
"outDir": "dist"
},
"exclude": [
"node_modules"
]
}
Alternatively, you can create this file by running the following command:
npx tsc --init
This file defines the TypeScript compiler options for your project, including the target version of ECMAScript, the module system, and the output directory.
Create a new directory called "src" in the root of your project directory. This directory will contain your TypeScript source code. Create a new file called "index.ts" in the "src" directory with the following content:
console.log("Hello World!");
Lastly, we'll test if everything is working by running the following command in your terminal:
npx tsc
This will compile your TypeScript source code into JavaScript and output it to the "dist" directory. You can run the compiled JavaScript file by running the following command:
node dist/index.js
If everything is working, you should see the following output in your terminal:
Hello World!
Installing Nokron
To install Nokron, run the following command in your terminal:
npm install --save @nokron/core
This will install the Nokron core package and add it to your project's dependencies.
Using Nokron
Now that you have installed Nokron, you can start using it in your project. This chapter will show you how to create a simple new Nokron application and how to add a controller to it.
Setting up the application
As the first step, you need to create a new Nokron application. To do this, we'll add the following code to the "index.ts" file in the "src" directory:
import { ApplicationFactory, DefaultApplication } from '@nokron/core';
async function run() {
const app = await ApplicationFactory.create(DefaultApplication);
await app.init();
await app.listen(3000, () => {
console.log('App is running on port 3000');
});
}
run();
This code creates a new Nokron application and starts it on port 3000. You can now run the application by running the following command in your terminal:
npx tsc && node dist/index.js
Optionally, this command can also be added to the "scripts" section of your package.json file, so it can be run more easily:
{
"scripts": {
"start": "npx tsc && node dist/index.js"
}
}
Now we can run the application by running the following command in your terminal:
npm start
If everything is working, you should see the following output in your terminal:
App is running on port 3000
The app is now running, but it doesn't do anything yet. Let's add a module and a controller to it.
Creating a module
Modules are the building blocks of Nokron applications. They contain all the logic for a specific part or feature of your application. To create a new module, create a new file called "my-module.ts" in the "src" directory with the following content:
import { Module } from '@nokron/core';
export const MyModule = Module({
name: 'MyModule',
controllers: [],
});
This code creates a new module with the name "MyModule". Modules can contain one or more controllers, which we'll add in the next step. However, we need to add this module to our application first. To do this, we'll add the following code to the "index.ts" file in the "src" directory:
import { ApplicationFactory, DefaultApplication } from '@nokron/core';
import { MyModule } from './my-module';
async function run() {
const app = await ApplicationFactory.create(DefaultApplication);
app.registerModules([MyModule]);
await app.init();
await app.listen(3000, () => {
console.log('App is running on port 3000');
});
}
This code registeres the "MyModule" module with the application. But the application still doesn't do much. So let's add a controller to it.
Adding a controller
Controllers are responsible for handling incoming requests. To create a new controller, create a new file called "cat-controller.ts" in the "src". As an example, this controller will return a list of cats when a GET request is sent to the "/cats" endpoint. To do this, we'll add the following code to the "cat-controller.ts" file:
import { Controller, HttpGet, Ok } from '@nokron/core';
@Controller('/cats')
export class CatController {
@HttpGet('/')
public async getCats() {
return Ok([
{ name: 'Garfield', age: 40 },
{ name: 'Tom', age: 45 },
]);
}
}
Now we need to add this controller to the "MyModule" module. To do this, we'll add the following code to the "my-module.ts" file:
import { Module } from '@nokron/core';
import { CatController } from './cat-controller';
export const MyModule = Module({
name: 'MyModule',
controllers: [CatController],
});
This code adds the "CatController" controller to the "MyModule" module. Now we can run the application again by running npm start
. If everything is working, we can test the controller by sending a GET request to the "/cats" endpoint. You can do this by running the following command in your terminal:
curl http://localhost:3000/cats
Which should return the following response:
[
{
"name": "Garfield",
"age": 40
},
{
"name": "Tom",
"age": 45
}
]
Next steps
Now that you have created your first Nokron application, you can start building more complex applications. You can find more information about Nokron in the following chapters, which will cover more advanced topics such as dependency injection, middleware, and more.