溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Node.js中的Web框架和工具有哪些

發(fā)布時間:2022-02-22 09:14:00 來源:億速云 閱讀:126 作者:iii 欄目:web開發(fā)

這篇文章主要介紹了Node.js中的Web框架和工具有哪些的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Node.js中的Web框架和工具有哪些文章都會有所收獲,下面我們一起來看看吧。

Node.js中的Web框架和工具有哪些

Node.js是一個底層平臺。為了方便開發(fā)者的工作變得簡單高效,社區(qū)誕生了超過上千個庫。

隨著時間的推移,有很多優(yōu)秀的庫可以供大家選擇,下面是不完全選擇列表:

  • Express: 提供非常簡單的方式來創(chuàng)建Web服務(wù)器,且功能足夠強(qiáng)大并且足夠的輕量,專注于服務(wù)器的核心功能。

    // server.js
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      res.send('Hello World!')
    })
    
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}`)
    })
  • koa: 它是由Express背后的同一團(tuán)隊打造,提供更加簡單、更小巧的庫,并且支持ES NEXT的async await語法。

    // server.js
    const Koa = require('koa');
    const app = new Koa();
    
    app.use(async ctx => {
      ctx.body = 'Hello World';
    });
    
    app.listen(3000);
  • NestJS: 一個基于TypeScript的漸進(jìn)式Node.js框架,用于構(gòu)建高效、可靠、可擴(kuò)展的企業(yè)級服務(wù)端應(yīng)用程序。

    // app.controller.ts
    import { Get, Controller, Render } from '@nestjs/common';
    import { AppService } from './app.service';
    
    @Controller()
    export class AppController {
        constructor(private readonly appService: AppService) {}
    
        @Get()
        @Render('index')
        render() {
            const message = this.appService.getHello();
            return { message };
        }
    }
  • Egg.js: 使用Node.js和Koa構(gòu)建更好的企業(yè)級服務(wù)端框架。

    // app/controller/home.js
    const Controller = require('egg').Controller;
    
    class HomeController extends Controller {
      async index() {
        this.ctx.body = 'Hello world';
      }
    }
    
    module.exports = HomeController;
  • Next.js: React 框架提供了良好的開發(fā)體驗,提供生產(chǎn)環(huán)境的所有功能:服務(wù)端渲染、支持TypeScript、路由預(yù)獲取等等。

    // first-post.js
    export default function FirstPost() {
      return <h2>First Post</h2>
    }
  • Remix: Remix 是一個全棧Web框架,它開箱即用,包含構(gòu)建現(xiàn)代Web應(yīng)用程序前端和后端。

    // index.tsx
    export async function loader({ request }) {
      return getProjects();
    }
    
    export async function action({ request }) {
      const form = await request.formData();
      return createProject({ title: form.get("title") });
    }
    
    export default function Projects() {
      const projects = useLoaderData();
      const { state } = useTransition();
      const busy = state === "submitting";
    
      return (
        <div>
          {projects.map((project) => (
            <Link to={project.slug}>{project.title}</Link>
          ))}
    
          <Form method="post">
            <input name="title" />
            <button type="submit" disabled={busy}>
              {busy ? "Creating..." : "Create New Project"}
            </button>
          </Form>
        </div>
      );
    }
  • Gatsby: 一個基于React、GraphQL的靜態(tài)站點生成器,具有非常豐富的插件和生態(tài)。

    // src/pages/index.js
    import * as React from 'react'
    
    const IndexPage = () => {
      return (
        <main>
          <title>Home Page</title>
          <h2>Welcome to my Gatsby site!</h2>
          <p>I'm making this by following the Gatsby Tutorial.</p>
        </main>
      )
    }
    
    export default IndexPage
  • hapi: 用于構(gòu)建Web應(yīng)用服務(wù)的框架,使開發(fā)人員能夠?qū)W⒂诰帉懣芍赜玫膽?yīng)用程序邏輯,而不是花費時間構(gòu)建基礎(chǔ)設(shè)施。

    // index.js
    
    'use strict';
    
    const Hapi = require('@hapi/hapi');
    
    const init = async () => {
    
        const server = Hapi.server({
            port: 3000,
            host: 'localhost'
        });
    
        server.route({
            method: 'GET',
            path: '/',
            handler: (request, h) => {
                return 'Hello World!';
            }
        });
    
        await server.start();
        console.log('Server running on %s', server.info.uri);
    };
    
    process.on('unhandledRejection', (err) => {
        console.log(err);
        process.exit(1);
    });
    
    init();
  • Fastify: 一個高度專注于以最少的開銷和強(qiáng)大的插件架構(gòu),提供最佳的開發(fā)體驗的Web框架。Fastify是最快的Node.js Web框架之一。

    // server.js
    const fastify = require('fastify')({ logger: true })
    
    // Declare a route
    fastify.get('/', async (request, reply) => {
      return { hello: 'world' }
    })
    
    // Run the server!
    const start = async () => {
      try {
        await fastify.listen(3000)
      } catch (err) {
        fastify.log.error(err)
        process.exit(1)
      }
    }
    start()
  • AdonisJS: 一個基于TypeScript的全功能框架,高度關(guān)注開發(fā)人員的體驗和穩(wěn)定性。Adonis是最快的Node.js Web框架之一。

    // PostsController.js
    import Post from 'App/Models/Post'
    
    export default class PostsController {
    
      public async index () {
        return Post.all()
      }
    
      public async store ({ request }) {
        return request.body()
      }
    
    }
  • FeatherJS: Feathers是一個輕量級的Web框架,用于使用JavaScript或者TypeScript創(chuàng)建實時應(yīng)用程序和REST API。在幾分鐘內(nèi)構(gòu)建原型程序,在幾天內(nèi)構(gòu)建企業(yè)級應(yīng)用程序。

    // app.ts
    import feathers from '@feathersjs/feathers';
    
    interface Message {
      id?: number;
      text: string;
    }
    
    
    class MessageService {
      messages: Message[] = [];
    
      async find () {
        return this.messages;
      }
    
      async create (data: Pick<Message, 'text'>) {
        const message: Message = {
          id: this.messages.length,
          text: data.text
        }
    
        this.messages.push(message);
    
        return message;
      }
    }
    
    const app = feathers();
    
    app.use('messages', new MessageService());
    
    app.service('messages').on('created', (message: Message) => {
      console.log('A new message has been created', message);
    });
    
    const main = async () => {
      await app.service('messages').create({
        text: 'Hello Feathers'
      });
    
      await app.service('messages').create({
        text: 'Hello again'
      });
    
      const messages = await app.service('messages').find();
    
      console.log('All messages', messages);
    };
    
    main();
  • Loopback.io: 使構(gòu)建復(fù)雜集成的現(xiàn)代應(yīng)用程序變得更加容易。

    // hello.controller.ts
    import {get} from '@loopback/rest';
    export class HelloController {
      @get('/hello')
      hello(): string {
        return 'Hello world!';
      }
    }
  • Meteor: 一個非常強(qiáng)大的全??蚣?,提供同構(gòu)的方法來使用JavaScript構(gòu)建應(yīng)用程序,在客戶端和服務(wù)端共享代碼。以前提供全套的現(xiàn)成工具,現(xiàn)在與前端庫React、Vue和Angular集成。也可用于創(chuàng)建移動應(yīng)用程序。

  • Micro: 它提供非常輕量級的服務(wù)器來創(chuàng)建異步HTTP微服務(wù)。

    // index.js
    const https = require('https');
    const {run, send} = require('micro');
    
    const {key, cert, passphrase} = require('openssl-self-signed-certificate');
    
    const PORT = process.env.PORT || 3443;
    
    const options = {key, cert, passphrase};
    
    const microHttps = fn => https.createServer(options, (req, res) => run(req, res, fn));
    
    const server = microHttps(async (req, res) => {
        send(res, 200, {encrypted: req.client.encrypted});
    });
    
    server.listen(PORT);
    console.log(`Listening on https://localhost:${PORT}`);
  • Nx: 使用NestJS、Express、React、Angular等進(jìn)行全棧monorepo開發(fā)的工具包,Nx有助于將您的開發(fā)從一個團(tuán)隊構(gòu)建一個應(yīng)用程序擴(kuò)展到多個團(tuán)隊協(xié)作開發(fā)多個應(yīng)用程序!

  • Sapper: Sapper是一個用于構(gòu)建各種規(guī)模的Web應(yīng)用程序框架,具有出色的開發(fā)體驗和靈活的基于文件系統(tǒng)的路由,提供SSR等等。

  • Socket.io: 用于構(gòu)建實時網(wǎng)絡(luò)應(yīng)用程序的WebSocket框架。

    // index.js
    const express = require('express');
    const app = express();
    const http = require('http');
    const server = http.createServer(app);
    const { Server } = require("socket.io");
    const io = new Server(server);
    
    app.get('/', (req, res) => {
      res.sendFile(__dirname + '/index.html');
    });
    
    io.on('connection', (socket) => {
      console.log('a user connected');
    });
    
    server.listen(3000, () => {
      console.log('listening on *:3000');
    });
  • Strapi: Strapi是一種靈活的開源無頭CMS,它讓開發(fā)人員可以自由選擇自己喜歡的工具和框架,同時允許編輯者輕松管理他們的內(nèi)容。

關(guān)于“Node.js中的Web框架和工具有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Node.js中的Web框架和工具有哪些”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI