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

来源:互联网 发布:note5网络锁 编辑:程序博客网 时间:2024/06/05 12:02

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

    • 41 功能与路由设计
      • 报错信息
      • 目录结构
    • 9 文章
      • 问题描述


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);

问题解决……

未完…..

原创粉丝点击