Skip to content

Getting Started

Cesar Blender edited this page Sep 13, 2023 · 6 revisions

Getting Started

This is a quick guide that explains how AstralJS work.

Project Structure

The AstralJS Framework is using the RCS (Router-Controller-Service) pattern, that is based on MVC (Model-View-Controller) pattern.

  • Router: Here you define the route, e.g: the route path, the middlewares, api version, body schema, if is a JWT-Protected route, etc.
  • Controller: Here you define all business logic, e.g: manipulate the request body, call the Service to execute database actions, etc.
  • Service: Here you define all database-related actions.

This is the file structure

  • src/
    • server.ts <-- Here you define the core of your server, and all of your main settings.
    • router/
      • index.ts <-- Here you register all the routes
      • /routes <-- Here you define all the routes
        • index.route.ts
      • /controllers <-- Here you define all the controllers
        • index.controller.ts
      • /services <-- Here you define all the services
        • index.service.ts

Explaining server.ts

Here you define the core of your server

import { createServer } from "@astralstack/astraljs";

import { endpoints } from "./router";

const server = createServer({
  endpoints,
});

server.start();

This is a basic server. by default it uses port 3000, but you can change it in settings

const server = createServer({
  port: 5000 /* e.g: 5000, or 8000, whatever port you want */,
});

To view all the settings avaliable click here.

Defining router

In src/router/index.ts you will find something like this

import { Endpoints } from "@astralstack/astraljs";

import { homeRoute } from "./routes/index.route";

export const endpoints: Endpoints = [
  homeRoute,
  /* Here you register all of your routes*/
];

Defining a route

To define a route, create a file in src/router/routes/<your route>.route.ts (replace <your route> with the name of your route!!!)

This is an example of a route

import { get, parseUrl } from "@astralstack/astraljs";

import { homeController } from "../controllers/index.controller";

export const homeRoute = get({
  settings: {
    path: parseUrl("/"),
  },
  controller: homeController,
});

To view all route settings click here

Defining a controller

To define a controller, do something like this:

Create a file in src/router/controllers/<the name of your controller>.controller.ts

import { Controller, HTTPMethods } from "@astralstack/astraljs";

import { getMyData } from "../services/index.service.ts";

// Controller<BodyType>
// BodyType is to specify the type of your request body, we will explain that on another article
export const homeController: Controller<null> = async (params) => {
  const myData = await getMyData();

  return {
    data: myData,
    message: "A hello world message from AstralJS", // An optional message to give the client a description of the response (in case of need it!)
    status: HTTPMethods.OK,
  };
};

Defining a service

to define a service, create a file in src/router/services/<the name of your service>.service.ts

export async function getMyData() {
    // This is to simulate a data obtention from a database
    // Here you will call e.g. MongoDB, MySQL, SQLite, etc.

    await new Promise((resolve) => setTimeOut(resolve, 3000))

    return {
        helloMesage: "Hello, World! From AstralJS!!"
    }
}

What's next?

To continue learning how AstralJS works, click here

Clone this wiki locally