对于初学者来说,nodejs就是用js能实现服务端代码,那么,实现一个最基本的web服务就是最重要的开始。
列举出各类框架的web服务端的写法,这里省略了程序初始化和依赖安装, 以下文件任选其一,命名为app.js,写完代码后在命令行使用node app.js即可启动。使用localhost:3000访问。
1. Express.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
2. Koa.js
const Koa = require('koa');
const app = new Koa();
const port = 3000;
app.use(async (ctx) => {
ctx.body = 'Hello, World!';
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
3. Hapi.js
const Hapi = require('@hapi/hapi');
const port = 3000;
const init = async () => {
const server = Hapi.server({
port,
host: 'localhost',
});
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello, World!';
},
});
await server.start();
console.log(`Server listening at ${server.info.uri}`);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
4. Fastify.js
const fastify = require('fastify')({ logger: true });
const port = 3000;
fastify.get('/', async (request, reply) => {
return 'Hello, World!';
});
const start = async () => {
try {
await fastify.listen(port);
fastify.log.info(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
5. Restify.js
const restify = require('restify');
const server = restify.createServer();
const port = 3000;
server.get('/', (req, res, next) => {
res.send('Hello, World!');
return next();
});
server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
6. InversifyJS
import { Container, injectable, inject } from 'inversify';
@injectable()
class Warrior {
public fight() { return 'fighting'; }
}
@injectable()
class Weapon {
public hit() { return 'hit'; }
}
@injectable()
class Samurai implements Warrior {
private _weapon: Weapon;
public constructor(@inject('Weapon') weapon: Weapon) {
this._weapon = weapon;
}
public fight() { return this._weapon.hit(); }
}
const container = new Container();
container.bind<Warrior>('Warrior').to(Samurai);
container.bind<Weapon>('Weapon').to(Weapon);
const warrior = container.get<Warrior>('Warrior');
console.log(warrior.fight()); // Output: hit
7. NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
8. Awilix
Awilix 是一个轻量级的依赖注入库,它使用容器来管理依赖项,但他本身没有启动web服务的功能,所以只能依附在其他框架下,下面以koa+awilix为例
const { asClass, asValue, createContainer } = require('awilix')
const { scopePerRequest } = require('awilix-koa')
const container = createContainer()
container.register({
// Scoped lifetime = new instance per request
// Imagine the TodosService needs a `user`.
// class TodosService { constructor({ user }) { } }
todosService: asClass(TodosService).scoped()
})
// Add the middleware, passing it your Awilix container.
// This will attach a scoped container on the context.
app.use(scopePerRequest(container))
// Now you can add request-specific data to the scope.
app.use((ctx, next) => {
ctx.state.container.register({
user: asValue(ctx.state.user) // from some authentication middleware..
})
return next()
})