《N-blog》学习笔记......

nswbmw大佬写的《N-blog - 使用 Express + MongoDB 搭建多人博客》学习笔记~~


4.4.1 功能与路由设计

报错信息……

报错信息

目录结构

目录结构

发现 config/default.js 中 session.maxAge 数据类型写错了, 不应该是字符串, 应该是数字

config

maxAge

Specifies the number (in seconds) to be the value for the Max-Age Set-Cookie attribute. The given number will be converted to an integer by rounding down. By default, no maximum age is set.

https://www.npmjs.com/package/cookie#maxage

另外 log 信息中前两行, 关于 express-session 的警告信息

resave: (是否允许)当客户端并行发送多个请求时, 其中一个请求在另一个请求结束时对session进行修改覆盖并保存, 默认为true. 但是(后续版本)有可能默认失效, 所以最好手动添加.

resave

Forces the session to be saved back to the session store, even if the session was never modified during the request. Depending on your store this may be necessary, but it can also create race conditions where a client makes two parallel requests to your server and changes made to the session in one request may get overwritten when the other request ends, even if it made no changes (this behavior also depends on what store you’re using).

The default value is true, but using the default has been deprecated, as the default will change in the future. Please research into this setting and choose what is appropriate to your use-case. Typically, you’ll want false.

How do I know if this is necessary for my store? The best way to know is to check with your store if it implements the touch method. If it does, then you can safely set resave: false. If it does not implement the touch method and your store sets an expiration date on stored sessions, then you likely need resave: true.

https://www.npmjs.com/package/express-session#resave

saveUninitialized: 初始化session时是否保存到存储, 默认为true, 但是(后续版本)有可能默认失效, 所以最好手动添加.

saveUninitialized

Forces a session that is “uninitialized” to be saved to the store. A session is uninitialized when it is new but not modified. Choosing false is useful for implementing login sessions, reducing server storage usage, or complying with laws that require permission before setting a cookie. Choosing false will also help with race conditions where a client makes multiple parallel requests without a session.

The default value is true, but using the default has been deprecated, as the default will change in the future. Please research into this setting and choose what is appropriate to your use-case.

https://www.npmjs.com/package/express-session#saveuninitialized

var express = require('express');
var session = require('express-session');
var config = require('config-lite')(__dirname); // (__dirname) 为 2.0 版本新写法

var app = express();

// ...

app.use(session({
    resave: true, // 强制更新 session
    saveUninitialized: false, // 设置为 false,强制创建一个 session,即使用户未登录
    name: config.session.key, //设置 cookie 中保存 session id 的字段名字
    secret: config.session.secret, // 通过设置 secret 来计算 hash 值并放在 cookie 中, 使产生的 signedCookie 防篡改
    cookie: {
        maxAge: config.session.maxAge // 过期时间, 过期后 cookie 中的 session id 自动删除
    },
    store: new MongoStore({ // 讲 session 存储到 mongodb
        url: config.mongodb // mongodb 地址
    })
}));

4.9 文章

问题描述……

数据总是自动保存到 test 数据库, 而不是指定的 myblog 数据库, 如图:

数据库截图

查阅资料发现, 如果不指定数据库, 将自动连接 test 数据库

var config = require('config-lite');
var Mongolass = require('mongolass');
var mongolass = new Mongolass();
mongolass.connect(config.mongodb);

但是如上, 已配置过数据库的连接, 打印 config.mongodb, 得到 undefined, 查询得知 v2 需指定根目录, npm 链接

var config = require('config-lite')(__dirname);

问题解决……

未完…..

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值