Overview
This guide will walk you through creating a basic HTTP server using AdonisJS Core. You’ll learn how to use the Ignitor class to bootstrap your application and define routes.This quickstart assumes you’ve already installed AdonisJS Core. If not, please complete the installation first.
Create Your First Server
Let’s build a simple HTTP server that responds to requests. Follow these steps to get up and running.Create the server entry point
Create a file at
bin/server.ts with the following code:bin/server.ts
The
Ignitor class is the entry point for AdonisJS applications. It handles bootstrapping your app in different environments: web, console, test, or REPL.Add routes
Modify your
bin/server.ts to add routes using the tap method:bin/server.ts
The
tap method allows you to access the application instance before it boots. The booting lifecycle hook runs during application initialization.Understanding the Code
Let’s break down the key components:The Ignitor Class
TheIgnitor class from @adonisjs/core is the application bootstrapper:
src/ignitor/main.ts:36, the Ignitor provides methods for different application modes:
httpServer()- Start an HTTP server for web requestsace()- Run CLI commandstestRunner()- Execute tests
View Ignitor source code (src/ignitor/main.ts)
View Ignitor source code (src/ignitor/main.ts)
HTTP Server Process
When you callignitor.httpServer().start(), the HttpServerProcess class (from src/ignitor/http.ts:32) handles:
- Creating the application in ‘web’ environment
- Initializing and booting the app
- Resolving the HTTP server from the container
- Creating a Node.js HTTP server
- Listening on the configured host and port
- Monitoring the server for graceful shutdown
View HTTP Server Process source (src/ignitor/http.ts)
View HTTP Server Process source (src/ignitor/http.ts)
Application Lifecycle
Thetap method allows you to hook into the application lifecycle:
Adding More Features
Now that you have a basic server running, let’s explore additional features:Route Groups
Organize related routes together:Middleware
Add middleware to routes:Request Validation
Validate incoming requests (requires@vinejs/vine):
Error Handling
Handle errors gracefully:Production Build
To build your application for production:Next Steps
Congratulations! You’ve created your first AdonisJS HTTP server. Here’s what to explore next:Routing
Learn about advanced routing features, named routes, and route resources
Controllers
Organize your route handlers into controller classes
Middleware
Create reusable middleware for authentication, CORS, and more
Validation
Validate requests with VineJS integration
Application Lifecycle
Understand the boot process and lifecycle hooks
Service Providers
Learn how to register and configure application services
Common Patterns
Separate route files
Separate route files
For larger applications, move routes to a separate file:Then import it in your
start/routes.ts
bin/server.ts:bin/server.ts
Custom server configuration
Custom server configuration
You can pass a custom server factory to handle HTTPS:
Graceful shutdown
Graceful shutdown
The HTTP server handles graceful shutdown automatically:
Need Help?
Join our community or open an issue on GitHub if you need assistance.