Bootstrapping
Setting up the web server

Bootstrapping

Overview

To bootstrap your server use the init method:
1
init(boostrapModule: any, rootPath?: string, authProvider?: AuthProvider,httpsConfiguration:any)
Copied!
bootstrapModule - the init module which use as entry point, contains references to other modules if needed.
rootPath - Server uri prefix
authProvider - the authorization services, more on that on Authorization section
httpsConfiguration - configuration for setting the connection as https (mostly use for - {cert:certificatePath,key:keyPath})

Simple usage

1
import {HttpServer} from "@sugoi/server";
2
3
const server:HttpServer = HttpServer.init(BootstrapModule,"/api")
4
.build()
5
.listen(3000);
Copied!

Setting middlewares and Error handlers

For setting static file serving use:
1
setStatic(pathToStatic:string,route?:string)
Copied!
For setting middlewares use:
1
setMiddlewares(...(app)=>void)
Copied!
For setting error handlers use:
1
setErrorHandlers((app) => void)
Copied!

Predefined error handler

SugoiJS provides a predefined error handler named (defaultErrorHandler) which provides loggin the error and returning an error to the customer.
Example:
1
app.use(defaultErrorHandler(<boolean>isDev))
Copied!
Full example:
1
(<HttpServer>server)
2
.setStatic("assets/admin","/admin")
3
.setStatic("assets/main")
4
.setMiddlewares((app) => {
5
app.use(bodyParser.json());
6
app.use(compression());
7
if (DEVELOPMENT) {
8
app.set('json spaces', 2);
9
app.use(cors());
10
app.use(require('morgan')('dev'));
11
}
12
app.use(express.static(paths.static));
13
})
14
.setErrorHandlers((app) => {
15
app.use(function (req, res, next) {
16
return res.sendFile(path.resolve(paths.index))
17
});
18
app.use(defaultErrorHandler(isDev));
19
});
Copied!

Build & listen

After setting the middlewares and error handlers, build and listen to requests by:
1
server.build()
2
.listen(PORT, (error: Error) => {
3
if (error) {
4
logger.error(error.message);
5
throw error;
6
}
7
logger.debug(`Server running @ ${HOST}:${PORT}`);
8
});
Copied!
This call will return http.Server instance which can be use for setting app variables, socket server and more.

Full example

1
server.setStatic("assets")
2
.setMiddlewares((app) => {
3
app.use(bodyParser.json());
4
app.use(compression());
5
})
6
.setErrorHandlers((app) => {
7
app.use(function (req, res, next) {
8
return res.sendFile(path.resolve(paths.index))
9
});
10
app.use((req,res,next)=>{
11
return function(err){
12
if(err instanceof SugoiServerError){
13
console.log.error(err.stack);
14
console.log.error(`${err.message} ${JSON.stringify(err.data)});
15
res.status(500).send("Internal error");
16
}
17
}
18
});
19
});
Copied!

Retrieving the server app

For retrieving the server application use the HttpServer instance method getServer() , this method will return the common server application which will allow you to use .get,.put,.use etc..

Example

1
const httpServer = server.build();
2
const app = httpServer.getServer();
3
app.get("/",(req,res,next)=>{
4
res.json({success:true,data:{}});
5
})
6
7
httpServer.listen(3000)
Copied!
SugoiJS support migrate existing project by providing hybrid mode.
For achieving this approach use the initializeFrom method
HttpServer.initializeFrom(sourceApp: TServer, bootstrapModule: any, authProvider?: TNewable<AuthProvider>)
TServer - http.Server | https.Server | { listen: (...args) => any }

Example

1
import {HttpServer} from "@sugoi/server";
2
3
const server:HttpServer = HttpServer.initializeFrom(myExpressApp,ServerModule);
Copied!

Last modified 2yr ago