简体   繁体   English

是否有必要使用express.js框架来开发nodeJS Web应用程序

[英]Is it necessary the express.js framework to develop nodeJS web application

I am trying to develop the simple web application by using HTML, NodeJS, and Postgres. 我正在尝试使用HTML,NodeJS和Postgres开发简单的Web应用程序。 is it possible to develop the web application without using ExpressJS. 是否可以在不使用ExpressJS的情况下开发Web应用程序。 Please let me know how to do the basic CURD operations by using NodeJs, Javascript, and Postgres without using the ExpressJS framework. 请告诉我如何使用NodeJs,Javascript和Postgres进行基本的CURD操作,而不使用ExpressJS框架。 Thanks in advance. 提前致谢。

Yes, you can skip express But It ain't gonna be easy. 是的,你可以跳过快递但这不容易。

But there is something else : 但还有别的东西:

is it possible to develop the web application without using ExpressJS 是否可以在不使用ExpressJS的情况下开发Web应用程序

It can mean, 这可能意味着,

  • you either use other frameworks , or 你要么使用其他框架 ,要么
  • Node.js web app from scratch Node.js Web应用程序从头开始

In the first case, you have options to choose from. 在第一种情况下,您可以选择。 There are MVC frameworks like hapi, Flicker.js etc, REST API frameworks like restify, loopback etc 有像hapi,Flicker.js等MVC框架,REST API框架,如restify,loopback等

In the second case, you are faced with the job of writing custom implementations of many features provided by express. 在第二种情况下,您将面临编写Express提供的许多功能的自定义实现的工作。

I'm continuing Assuming you meant the second case... 我继续假设你的意思是第二个案例......

For creating the HTTP server, you can use node's HTTP Module 要创建HTTP服务器,可以使用节点的HTTP模块

var http = require('http');

http.createServer(function (req, res) {
  res.write('Hello World!'); 
  res.end(); 
}).listen(8080); 

This method makes it a pain to write a REST API 这种方法使编写REST API变得很麻烦

req.url; //the request URL
if(url == '/users/create')
{
    //do this
}
else if(url == '/users/details')
{
    //do that
}

To connect to postgres, you have to use node-postgres (pg) anyways ( pg documentation ) 要连接到postgres,你必须使用node-postgres(pg)( pg文档

const { Client } = require('pg')
const client = new Client()

await client.connect()

const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()

As for the CRUD operations, apart from their API implementation, the code is going to be almost the same. 至于CRUD操作,除了API实现之外,代码几乎是一样的。 So you can refer to the tutorials that use express. 所以你可以参考使用express的教​​程。

https://mherman.org/blog/postgresql-and-nodejs/ https://mherman.org/blog/postgresql-and-nodejs/

It is not necessary to use Express. 没有必要使用Express。 Express is just a library that uses Node-Functions to make it easier for you to implement a Webserver. Express只是一个使用节点功能的库,使您可以更轻松地实现Web服务器。

There are plenty alternatives out there: A small overview of the 'best' 那里有很多替代方案: “最佳”的小概述

For the CURD Operations see the documentation of the Frameworks. 对于CURD操作,请参阅框架的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM