在开发时,将此标记用于与Web开发服务器相关的问题,即Web应用程序的后端/服务器端。
import express from "express"; import bodyParser from "body-parser"; import pg from "pg"; const app = express(); const port = 3000; const db = new pg.Client({ user: "postgres", host: "localhost", database: "world", password: "12345678", port: 5432, }); db.connect(); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static("public")); async function checkVisisted() { const result = await db.query("SELECT country_code FROM visited_countries"); let countries = []; result.rows.forEach((country) => { countries.push(country.country_code); }); return countries; } // GET home page app.get("/", async (req, res) => { const countries = await checkVisisted(); res.render("index.ejs", { countries: countries, total: countries.length }); }); //INSERT new country app.post("/add", async (req, res) => { const input = req.body["country"]; const result = await db.query( "SELECT country_code FROM countries WHERE country_name = $1", [input] ); if (result.rows.length !== 0) { const data = result.rows[0]; const countryCode = data.country_code; const final_data = await db.query("INSERT INTO visited_countries (country_code) VALUES ($1)", [ countryCode, ]); res.redirect("/"); } }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });`
<!-- wp:html --> <form id="webjob-form" enctype="multipart/form-data"> <input type="file" id="file" name="file" required> <button type="submit">Run WebJob</button> </form> <div id="output"></div> <img id="stitched-image" style="max-width: 100%; display: none;" /> <script> document.getElementById('webjob-form').addEventListener('submit', async (e) => { e.preventDefault(); const formData = new FormData(); formData.append('file', document.getElementById('file').files[0]); const response = await fetch('<?php echo admin_url('admin-ajax.php? action=trigger_webjob'); ?>', { method: 'POST', body: formData }); const result = await response.json(); if (result.success) { // Display output or stitched image const imageUrl = result.data.match(/https:\/\/[^\s]+/)[0]; // Extract URL from response document.getElementById('stitched-image').src = imageUrl; document.getElementById('stitched-image').style.display = 'block'; } else { document.getElementById('output').innerHTML = `<pre style="color: red;">Failed: ${result.data}</pre>`; } }); </script>
反应被CORB(交叉读取阻塞)阻止:AppWrite +React
贝洛代码是我使用image
如何处理用户在python中使用烧瓶登录? 我正在开发带有Python烧瓶的基本Web应用程序,我正在尝试根据是否有登录用户来修改我使用的一些模板。我正在使用MongoDB来存储用户数据。 下面...
auth_blueprint = Blueprint('auth', __name__, template_folder='templates') # Setup Flask-Login and Bcrypt bcrypt = Bcrypt() login_manager = LoginManager() login_manager.login_view = "auth.login" # MongoDB configuration mongo_uri = config.mongo_uri client = MongoClient(mongo_uri) db = client[config.DB_NAME] users_collection = db["users"] # User class for Flask-Login class User(UserMixin): def __init__(self, id, username, password): self.id = id self.username = username self.password = password @staticmethod def get(user_id): user_data = users_collection.find_one({"_id": user_id}) if user_data: return User(str(user_data["_id"]), user_data["username"], user_data["password"]) return None def get_id(self): return self.id @login_manager.user_loader def load_user(user_id): print(f'load_user method is called and the user_id is {user_id}') try: return User.get(user_id) except: return None @auth_blueprint.route("/register", methods=["GET", "POST"]) def register(): if request.method == "POST": username = request.form["username"] password = request.form["password"] confirmed_password = request.form["repeat-password"] if not username: flash("Please enter a username.", "danger") return redirect(url_for("auth.register")) if users_collection.find_one({"username": username}): flash("Username already exists!", "danger") return redirect(url_for("auth.register")) if not password: flash("Please enter a password.", "danger") return redirect(url_for("auth.register")) if not confirmed_password: flash("Please confirm the password.", "danger") return redirect(url_for("auth.register")) if password != confirmed_password: flash("Passwords must match!", "danger") return redirect(url_for("auth.register")) hashed_password = bcrypt.generate_password_hash(password).decode("utf-8") users_collection.insert_one({"username": username, "password": hashed_password}) flash("Registration successful!", "success") return redirect(url_for("auth.login")) return render_template("register.html") @auth_blueprint.route("/login", methods=["GET", "POST"]) def login(): if request.method == "POST": username = request.form["username"] password = request.form["password"] # Check if the user exists and credentials are valid user_data = users_collection.find_one({"username": username}) print(user_data) if user_data and bcrypt.check_password_hash(user_data["password"], password): session.clear() user = User(str(user_data['_id']), user_data['username'], user_data['password']) print(f'User id is {user.id}') result = login_user(user, remember=True, duration=timedelta(days=7), force=True) session.modified = True print(f'Is user authenticated in login function: {current_user._get_current_object().is_authenticated}') # print(f'Current user type is {type(current_user.is_authenticated)}') if result: print("Loggin successful!") #print(f"Session after login: {session}") # Inspect session object flash("Login successful!", "success") return redirect(url_for("index")) else: print("Something went wrong :(") return redirect(url_for("auth.login")) # session['username'] = user.username # if not helpers.url_has_allowed_host_and_scheme(next, request.host): # return abort(400) else: flash("Invalid credentials!", "danger") return render_template("login.html") @auth_blueprint.route("/check_login") def check_login(): print(f'Is user authenticated in check_login function: {current_user._get_current_object().is_authenticated}') print(type(current_user._get_current_object())) if current_user._get_current_object().is_authenticated: return f"User {current_user.name} is logged in." else: return "No user is logged in."
来自“超级英雄”的Import超级英雄;
llet说我有一个格式(用JSX编写)发送到Express服务器: <form id="log-in-form-itself" action="/submit-form" method="post"> <label htmlFor="username" >Username</label> <input type="text" id="username" name="username" required></input> <label htmlFor="password">Password</label> <input type="password" id="password" name="password" required></input> <button id="log-in-button">Log In</button> </form> 我知道该表格将使用“/提交形式”路线进入服务器。该路线将在Express这样处理: app.post("/submit-form", (req, res) => { //some form processing. Doesn't matter. res.send("some value"); }); 我的问题是,如何在我的前端代码中访问“ res.send()”的值?这不是一个普通的获取请求,我可以使用响应对象。邮政请求本身以形式嵌入,那么我如何访问其响应? 为了清楚起见,我正在为前端进行反应,并为后端表达。 回答您的问题: “ res.send”从默认表格内完成邮政请求时,“ res.send”在哪里?” 它通过HTTP响应返回到浏览器,这在后端上生成的所有内容(在您的情况下是通过Express),并通过res.send命令发送。除非您使用JS处理表单请求,否则您无法动态处理此响应。 本地HTML表单提交,意味着您正在执行HTTP POST请求,并从后端获得某种资源。
如何修复 Next.js 应用程序在成功调用 API 后不呈现动态内容?
我有一个 Next.js 应用程序,它在 getServerSideProps 中进行 API 调用以获取一些动态数据,然后将其呈现在页面上。然而API调用成功后,内容却不是
Python“忘记”变量? (Web 服务器上下文中的请求之间)
我正在开发一个项目,其核心是运行远程监控类型网站的服务器,以通过 LTE 监控汽车上单独仪表板系统的一些数据。汽车将发送其速度和
我正在开发一个项目,其核心是运行远程监控类型网站的服务器,以通过 LTE 监控汽车上单独仪表板系统的一些数据。汽车将发送其速度和
nextjs 的新手。 我正在学习 Max Schwarzmüller 的课程。 我有两个问题: 创建动态网址时,首先,我尝试将文件夹/页面创建为“博客”,每当尝试...
使用 JWT 进行基于令牌的身份验证,但我不确定如何在我的 PHP API 中实现它。有人可以提供代码示例吗?
我正在开发一个项目,用 PHP 构建 RESTful API,并希望实现基于令牌的身份验证。我读过有关使用 JWT(JSON Web 令牌)的信息,但不确定如何正确设置它。
我正在做一些项目,我丢失了一半的备份。所以我不得不重新开始。现在,对于备份的文件,我只是将它们复制并粘贴到新项目中。现在它给我一个裁判...
问题:- 我在制作 YouTube 克隆时无法使用切换和折叠属性? 我尝试过的:- 我已经采取了一个带有容器流体类的 div 容器,并且在该类中我采取了...
我正在尝试将值更新为传递给 neonInstance 我尝试使用不同的方法更改值,它确实更改了值,但 neonInstance 不接受给定的 v...
如何修复“paths[0]”参数必须为字符串类型。使用 ExpressJS 引入 Mustache 部分时收到“数组实例”错误?
index.js 记录视图路径以确保部分可见 从“快递”进口快递; 从“body-parser”导入 bodyParser; 从“路径”导入路径; 从 'url' 导入 { fileURLToPath }; 小鬼...
我正在尝试从头开始通过flask构建一个cors代理。这是我的代码 @app.route('/api/v1/cors/url=&method=',methods=['GET']) def api_cors(名称, 方法): 如果
我无法理解出了什么问题,我让程序下次继续运行,这不仅仅是给我一个内部服务器错误,我尝试自己修复问题,我从第一眼开始就尝试使用chatgpt...
为什么我的烧瓶程序一次运行一次却没有运行并且无法正确重定向页面
我不明白出了什么问题,我让程序下次继续运行,这不仅仅是给我一个内部服务器错误,我尝试自己修复问题,我从第一眼开始就尝试使用chatgpt...
背景:制作一个选择您自己的冒险类型游戏,但作为一个网站。根据选择 A 或 B(只有 2 个选择),故事将朝那个方向发展。 设置: 假设有 5 部电话,用于输入 ...