我一直在研究一个用于URL缩短的项目,每次有人点击该api或提出一个views
请求时,我都会尝试在其中递增get
。我不知道出了什么问题,但是视图只增加了一次。
这是我编写的用于每次获取请求时增加视图的代码。
我不知道出了什么问题,我已经搜索了很多论坛,stack overflow,articles已经问过问题。
注意-
views
的默认值为0
,here是post
请求中的值。这是文件树
├── 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
答案 0 :(得分:3)
简单来说:
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(临时)状态代码。
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 Repo创建了拉取请求,您可以进行验证。