[英]Need help understanding logic
Could someone please explain to me how this line works: https://github.com/sveltejs/realworld/blob/master/src/routes/login/index.svelte#L13有人可以向我解释这条线是如何工作的: https://github.com/sveltejs/realworld/blob/master/src/routes/login/index.svelte#L13
const response = await post(
auth/login , { email, password });
const response = await post(
身份验证/登录, { email, password });
post
is being called from utils.js
, which is this: post
是从utils.js
调用的,它是这样的:
utils.js实用程序.js
export function post(endpoint, data) {
return fetch(endpoint, {
method: 'POST',
credentials: 'include',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
}).then(r => r.json());
}
So the function enters here, and then fetches the endpoint provided, which was auth/login
.所以 function 进入这里,然后获取提供的端点,即auth/login
。
This confuses me because auth/login
is not an endpoint, it's a file that exports a function, under auth/login.js
.这让我很困惑,因为auth/login
不是端点,它是一个导出 function 的文件,位于auth/login.js
下。 Does this second post function in auth/login.js
get called automatically? auth/login.js
中的第二个帖子 function 会自动调用吗? I am unsure where this (req, res)
gets passed in as well, since we are just fetching this file from above and not passing any arguments.我也不确定这个(req, res)
是从哪里传入的,因为我们只是从上面获取这个文件,而不是传递任何 arguments。
auth/login.js身份验证/login.js
import * as api from 'api.js';
export function post(req, res) {
const user = req.body;
api.post('users/login', { user }).then(response => {
if (response.user) req.session.user = response.user;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
});
}
This is where the user is being set in a cookie, which my code isn't currently doing, and the session is lost upon refresh.这是在 cookie 中设置用户的地方,我的代码目前没有这样做,并且 session 在刷新时丢失。 I am trying to understand how to persist sessions in Sapper.我试图了解如何在 Sapper 中保持会话。
This line is making a call to a relative path: const response = await post( auth/login
, { email, password });此行正在调用相对路径: const response = await post( auth/login
, { email, password });
So the url that fetch is calling is something like: http://yourdomain.com/auth/login所以 fetch 调用的 url 类似于: http://yourdomain.com/auth/login
According to the docs, what happens when a route ending in.js is called is that Sapper looks for a function with the name of the HTTP request method on that file.根据文档,当调用以 in.js 结尾的路由时,Sapper 会在该文件上查找名称为 HTTP 请求方法的 function。 More info here: sapper.svelte.dev/docs#Server_routes更多信息:sapper.svelte.dev/docs#Server_routes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.