我正在尝试使用 HTML、NodeJS 和 Postgres 开发简单的 Web 应用程序。是否可以在不使用 ExpressJS 的情况下开发 Web 应用程序。 请告诉我如何在不使用 ExpressJS 框架的情况下使用 NodeJs、Javascript 和 Postgres 执行基本的 CURD 操作。 提前致谢。
最佳答案
是的,您可以跳过 express 但这并不容易。
但是还有别的东西:
is it possible to develop the web application without using ExpressJS
这可能意味着,
- 您要么使用其他框架,要么
- Node.js 网络应用从头开始
在第一种情况下,您有多种选择。有hapi、Flicker.js等MVC框架,restify、loopback等REST API框架
在第二种情况下,您面临着为 express 提供的许多功能编写自定义实现的工作。
我继续假设你指的是第二种情况......
要创建 HTTP 服务器,您可以使用 Node 的 HTTP Module
var http = require('http');
http.createServer(function (req, res) {
res.write('Hello World!');
res.end();
}).listen(8080);
这种方法让编写 REST API 变得很痛苦
req.url; //the request URL
if(url == '/users/create')
{
//do this
}
else if(url == '/users/details')
{
//do that
}
要连接到 postgres,无论如何你都必须使用 node-postgres (pg) (pg documentation)
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()
至于 CRUD 操作,除了它们的 API 实现之外,代码将几乎相同。所以可以引用使用express的教程。
关于javascript - 开发nodeJS web应用是否需要express.js框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846771/