首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB值在递增一次后不递增

问MongoDB值在递增一次后不递增
EN

Stack Overflow用户
提问于 2019-10-17 08:38:01
回答 1查看 55关注 0票数 1

我一直在为url缩短器做一个项目,每当有人点击该api或有人提出views请求时,我都会尝试增加get。我不知道哪里出了问题,但这些观点只是在增加一次而已。

​

​

下面是我编写的代码,用于每次发出get请求时增加视图。

我不知道出了什么问题,,我已经搜索了很多论坛,堆栈溢出,文章已经问过问题了。

Note -

  • views的默认值是0,这里是post请求中的值。
  • 从现在起,由虚拟值生成的不同用户。
  • 猫鼬模式链接- 链接
  • 我正在使用nodejs、高速公路、mongodb(数据库)、mongoose(orm)、pugjs(查看引擎)创建这个项目。
  • 项目链接:链接

,这是文件树

代码语言:javascript
复制
├── LICENSE
├── README.md
├── client
│   ├── assets
│   │   ├── bg.svg
│   │   ├── favicon
│   │   │   └── favicon.ico
│   │   └── fonts
│   │       ├── Apercu\ Medium.woff
│   │       ├── Apercu\ Mono.woff
│   │       └── Apercu_Regular.woff
│   ├── css
│   │   └── style.css
│   ├── index.html
│   └── js
│       └── script.js
├── index.js
├── models
│   ├── admin_model.js
│   └── urlshorten.js
├── package-lock.json
├── package.json
├── routes
│   ├── admin.js
│   ├── auth.js
│   ├── custom.js
│   ├── stats.js
│   └── urlShorten.js
├── static
│   ├── css
│   │   └── style.css
│   ├── favicon.ico
│   ├── fonts
│   │   └── Inter-Regular.woff
│   └── urlshort.gif
└── views
    ├── index.pug
    └── script.js
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-17 09:09:21

简单地说:

代码语言:javascript
复制
1. It is because of 301 Status Code for Redirect you are using.
2. 301 Status Code represents a Permanent Redirect.
3. The browser stores the main URL in the memory.
4. The next time you request the short link from your browser, it gets the Original
   URL from its memory and sends you there, without going through the Server.
5. Since the request doesn't go through the server, the server fails to increment it.

解决方案:

将301 (永久)状态代码更改为307 (临时)状态代码。

代码语言:javascript
复制
FILE: ShortLink/routes/urlShortner.js

Change the below lines

Line 37:  res.writeHead(301, {
Line 38:      Location: url.inputUrl
Line 39:  });

to

Line 37:  res.writeHead(307, {
Line 38:      Location: url.inputUrl
Line 39:  });

我还创建了一个拉的请求,您可以验证你的Github回购。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58428329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档