
Express
亮子AI
全干程序员
展开
-
Node.js 中联合使用两个日志包:Morgan, Winston
代码:var logger = new winston.Logger({ transports: [ new winston.transports.File({ level: 'info', filename: './logs/all-logs.log', handleExceptions: true...原创 2020-03-08 18:08:57 · 826 阅读 · 0 评论 -
PostgreSQL 如何写条件:值在数组中、值不在数组中
值在数组中:SELECT COUNT(*) FROM "messages" WHERE 3 = ANY (recipient_ids)值不在数组中SELECT COUNT(*) FROM "messages" WHERE NOT (3 = ANY (recipient_ids))如果你用的是 node.js 的 pg 包,值在数组中可以这样写:var ids = [1,3,4]; ...原创 2020-03-06 20:42:05 · 8912 阅读 · 0 评论 -
node.js 上传文件比较 busboy vs. formidable vs. multer vs. multiparty
Multer 最流行,更新及时。formidable 下载量最大,更新迟缓。详细比较在这里:https://npmcompare.com/compare/busboy,formidable,multer,multiparty原创 2019-11-16 09:25:20 · 1203 阅读 · 0 评论 -
Express.js Hello World!
使用 Nginx ,配置如下: location /hello { rewrite ^/hello$ / break; rewrite ^/hello(.*)/$ /hello$1 permanent; # Remove trailing / rewrite ^/hello/(.*) /$1 break; proxy_pass http://127.0.0.1:...原创 2019-09-14 17:18:48 · 336 阅读 · 0 评论 -
正确理解 Expres.js 中的 Route 和中间件(middleware)的关系
在 StackOverflow 上看到的,解释比较详细,于是翻译了部分内容: //get the express module as app var app = require('express')(); //1. when this *middleware* is called, this is printed to the console, always. /...原创 2019-03-23 16:19:14 · 781 阅读 · 0 评论 -
Node.js pg-promise模块出现警告:Creating a duplicate database object for the same connection。怎样在模块中定义数据库连接?
nodejs 的模块 pg-promise 让我们方便使用 PostgreSQL。但是,就算在不同的模块中定义数据库连接,仍然会出现警告信息:WARNING: Creating a duplicate database object for the same connection.正确的做法是,在一个单独的模块中定义数据库连接,其他模块导入使用这个连接。数据库模块的写法:const i...原创 2019-02-25 20:36:14 · 509 阅读 · 0 评论 -
Node.js 如何获取真实 IP?(Nginx 服务器)
在 nginx.conf 中配置:proxy_set_header X-Real-IP $remote_addr;在 node.js 中获取 ip:const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;https://stackoverflow.com/a/18481747/3054511...原创 2019-02-25 13:47:47 · 1699 阅读 · 0 评论 -
Express,将中间件写在模块中
文件:my-middleware.jsmodule.exports = function(options) { return function(req, res, next) { // Implement the middleware function based on the options object next() }}使用这个中间件:var mw = re...原创 2019-02-19 14:19:32 · 331 阅读 · 0 评论 -
MySQL 查询中,如何做到大小写敏感?
默认是大小写不敏感。请在条件语句中加上 BINARY:SELECT * FROM `table` WHERE BINARY `column` = 'value'这个答案获得了很多人赞同,还有人质问:这正是我想要的答案,为什么这个回答没被选为正确答案?https://stackoverflow.com/a/5629121...原创 2018-10-27 22:06:14 · 293 阅读 · 0 评论 -
Express (version>=4.16.0) 怎样获取 post 的参数值?
直接上代码:const express = require("express")const bodyParser = require("body-parser")const app = express()app.use(bodyParser.json())app.use(bodyParser.urlencoded({ extended: true}))app.use(expre...原创 2018-10-27 21:24:42 · 691 阅读 · 0 评论 -
怎样查看 nodejs 安装包的版本信息?
查看当前路径的安装包:npm list查看全局的安装包:npm list -g查看某个安装包:$ npm list gruntprojectName@projectVersion /path/to/project/folder└── grunt@0.4.1或者:$ npm list | grep grunt└── grunt@0.4.1...原创 2018-10-27 20:58:57 · 9246 阅读 · 0 评论 -
Express 的中间件
中间件是 Express 强大的功能,它的处理流程示意图如下:下面是一个演示例子:const app = express()app.use((request, response, next) => { console.log(request.headers) next()})app.use((request, response, next) => { req...原创 2018-10-21 14:04:41 · 989 阅读 · 0 评论