首頁  >  文章  >  web前端  >  推薦分享Node.js中18個值得了解的Web框架與工具

推薦分享Node.js中18個值得了解的Web框架與工具

青灯夜游
青灯夜游轉載
2022-02-21 19:36:012727瀏覽

本篇文章是一個Web框架和工具分享文章,本文中為大家總結分享18個我最推薦的Node Web框架和工具,希望對大家有幫助!

推薦分享Node.js中18個值得了解的Web框架與工具

Node.js是一個底層平台。為了方便開發者的工作變得簡單高效,社群誕生了超過上千個函式庫。

隨著時間的推移,有許多優秀的函式庫可以供大家選擇,以下是不完全選擇清單:

  • Express: 提供非常簡單的方式來創建Web伺服器,且功能足夠強大且足夠的輕量,專注於伺服器的核心功能。

    // 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背後的同一團隊打造,提供更簡單、更小巧的庫,並且支援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的漸進式Node.js框架,用於建立高效能、可靠、可擴展的企業級服務端應用程式。

    // 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建立更好的企業級服務端框架。

    // 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 框架提供了良好的開發體驗,提供生產環境的所有功能:服務端渲染、支援TypeScript、路由預先取得等等。

    // first-post.js
    export default function FirstPost() {
      return <h1>First Post</h1>
    }
  • Remix: Remix 是一個全堆疊Web框架,它開箱即用,包含建立現代Web應用程式前端和後端。

    // 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的靜態網站產生器,具有非常豐富的插件和生態。

    // src/pages/index.js
    import * as React from &#39;react&#39;
    
    const IndexPage = () => {
      return (
        <main>
          <title>Home Page</title>
          <h1>Welcome to my Gatsby site!</h1>
          <p>I&#39;m making this by following the Gatsby Tutorial.</p>
        </main>
      )
    }
    
    export default IndexPage
  • hapi: 用於建立網路應用服務的框架,使開發人員能夠專注於編寫可重複使用的應用程式邏輯,而不是花費時間建構基礎設施。

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

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

    // PostsController.js
    import Post from &#39;App/Models/Post&#39;
    
    export default class PostsController {
    
      public async index () {
        return Post.all()
      }
    
      public async store ({ request }) {
        return request.body()
      }
    
    }
  • FeatherJS#: Feathers是一個輕量級的Web框架,用於使用JavaScript或TypeScript建立即時應用程式和REST API 。在幾分鐘內建立原型程序,在幾天內建立企業級應用程式。

    // app.ts
    import feathers from &#39;@feathersjs/feathers&#39;;
    
    interface Message {
      id?: number;
      text: string;
    }
    
    
    class MessageService {
      messages: Message[] = [];
    
      async find () {
        return this.messages;
      }
    
      async create (data: Pick<Message, &#39;text&#39;>) {
        const message: Message = {
          id: this.messages.length,
          text: data.text
        }
    
        this.messages.push(message);
    
        return message;
      }
    }
    
    const app = feathers();
    
    app.use(&#39;messages&#39;, new MessageService());
    
    app.service(&#39;messages&#39;).on(&#39;created&#39;, (message: Message) => {
      console.log(&#39;A new message has been created&#39;, message);
    });
    
    const main = async () => {
      await app.service(&#39;messages&#39;).create({
        text: &#39;Hello Feathers&#39;
      });
    
      await app.service(&#39;messages&#39;).create({
        text: &#39;Hello again&#39;
      });
    
      const messages = await app.service(&#39;messages&#39;).find();
    
      console.log(&#39;All messages&#39;, messages);
    };
    
    main();
  • Loopback.io#: 讓建立複雜整合的現代應用程式變得更加容易。

    // hello.controller.ts
    import {get} from &#39;@loopback/rest&#39;;
    export class HelloController {
      @get(&#39;/hello&#39;)
      hello(): string {
        return &#39;Hello world!&#39;;
      }
    }
  • Meteor: 一個非常強大的全端框架,提供同構的方法來使用JavaScript建立應用程序,在客戶端和服務端共享代碼。以前提供全套的現成工具,現在與前端庫React、Vue和Angular整合。也可用於建立行動應用程式。

  • Micro: 它提供非常輕量級的伺服器來建立非同步HTTP微服務。

    // index.js
    const https = require(&#39;https&#39;);
    const {run, send} = require(&#39;micro&#39;);
    
    const {key, cert, passphrase} = require(&#39;openssl-self-signed-certificate&#39;);
    
    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等进行全栈monorepo开发的工具包,Nx有助于将您的开发从一个团队构建一个应用程序扩展到多个团队协作开发多个应用程序!

  • Sapper: Sapper是一个用于构建各种规模的Web应用程序框架,具有出色的开发体验和灵活的基于文件系统的路由,提供SSR等等。

  • Socket.io: 用于构建实时网络应用程序的WebSocket框架。

    // index.js
    const express = require(&#39;express&#39;);
    const app = express();
    const http = require(&#39;http&#39;);
    const server = http.createServer(app);
    const { Server } = require("socket.io");
    const io = new Server(server);
    
    app.get(&#39;/&#39;, (req, res) => {
      res.sendFile(__dirname + &#39;/index.html&#39;);
    });
    
    io.on(&#39;connection&#39;, (socket) => {
      console.log(&#39;a user connected&#39;);
    });
    
    server.listen(3000, () => {
      console.log(&#39;listening on *:3000&#39;);
    });
  • Strapi: Strapi是一种灵活的开源无头CMS,它让开发人员可以自由选择自己喜欢的工具和框架,同时允许编辑者轻松管理他们的内容。

以上就是我推荐的Node.js Web框架和工具,如果有更好的推荐欢迎在评论区评论。

更多node相关知识,请访问:nodejs 教程

以上是推薦分享Node.js中18個值得了解的Web框架與工具的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.cn。如有侵權,請聯絡admin@php.cn刪除