当前位置: 首页 > news >正文

会小二也是做会议网站的google搜索排名优化

会小二也是做会议网站的,google搜索排名优化,竞价网站做不做链接,长春行业网站在现代 Web 开发中,前后端分离的架构已经成为主流。本文将详细介绍如何使用 Vue3、Node.js、MySQL、Electron 和 Express 实现一个完整的用户登录、文章管理和截屏功能的应用。我们将从项目的初始化开始,逐步实现各个功能模块,并提供详细的代…

在现代 Web 开发中,前后端分离的架构已经成为主流。本文将详细介绍如何使用 Vue3、Node.js、MySQL、Electron 和 Express 实现一个完整的用户登录、文章管理和截屏功能的应用。我们将从项目的初始化开始,逐步实现各个功能模块,并提供详细的代码示例。

项目初始化

前端:Vue3

首先,我们使用 Vue CLI 创建一个新的 Vue3 项目:

npm install -g @vue/cli
vue create vue-electron-app
cd vue-electron-app

选择默认配置或根据需要进行自定义配置。

后端:Node.js 和 Express

在项目根目录下创建一个新的文件夹 server,并在其中初始化一个新的 Node.js 项目:

mkdir server
cd server
npm init -y
npm install express mysql body-parser cors

创建 server.js 文件并设置基本的 Express 服务器:

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const port = 3000;app.use(cors());
app.use(bodyParser.json());app.listen(port, () => {console.log(`Server running on port ${port}`);
});

数据库:MySQL

创建一个新的 MySQL 数据库和表:

CREATE DATABASE vue_electron_app;USE vue_electron_app;CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL
);CREATE TABLE articles (id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255) NOT NULL,content TEXT NOT NULL,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

实现用户登录功能

后端:用户登录 API

server 文件夹中创建一个新的文件 auth.js,并实现用户注册和登录功能:

const express = require('express');
const router = express.Router();
const mysql = require('mysql');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');const db = mysql.createConnection({host: 'localhost',user: 'root',password: 'password',database: 'vue_electron_app'
});router.post('/register', (req, res) => {const { username, password } = req.body;const hashedPassword = bcrypt.hashSync(password, 10);db.query('INSERT INTO users (username, password) VALUES (?, ?)', [username, hashedPassword], (err, result) => {if (err) return res.status(500).send(err);res.status(201).send('User registered');});
});router.post('/login', (req, res) => {const { username, password } = req.body;db.query('SELECT * FROM users WHERE username = ?', [username], (err, results) => {if (err) return res.status(500).send(err);if (results.length === 0) return res.status(404).send('User not found');const user = results[0];const isPasswordValid = bcrypt.compareSync(password, user.password);if (!isPasswordValid) return res.status(401).send('Invalid password');const token = jwt.sign({ id: user.id }, 'secret_key', { expiresIn: '1h' });res.status(200).send({ token });});
});module.exports = router;

server.js 中引入并使用该路由:

const authRoutes = require('./auth');
app.use('/auth', authRoutes);

前端:用户登录页面

在 Vue 项目中创建一个新的组件 Login.vue

<template><div><h2>Login</h2><form @submit.prevent="login"><div><label for="username">Username:</label><input type="text" v-model="username" required /></div><div><label for="password">Password:</label><input type="password" v-model="password" required /></div><button type="submit">Login</button></form></div>
</template><script>
import axios from 'axios';export default {data() {return {username: '',password: ''};},methods: {async login() {try {const response = await axios.post('http://localhost:3000/auth/login', {username: this.username,password: this.password});localStorage.setItem('token', response.data.token);this.$router.push('/dashboard');} catch (error) {console.error('Login failed:', error);}}}
};
</script>

实现文章管理功能

后端:文章管理 API

server 文件夹中创建一个新的文件 articles.js,并实现文章的 CRUD 操作:

const express = require('express');
const router = express.Router();
const mysql = require('mysql');
const jwt = require('jsonwebtoken');const db = mysql.createConnection({host: 'localhost',user: 'root',password: 'password',database: 'vue_electron_app'
});const authenticate = (req, res, next) => {const token = req.headers['authorization'];if (!token) return res.status(401).send('Access denied');jwt.verify(token, 'secret_key', (err, decoded) => {if (err) return res.status(401).send('Invalid token');req.userId = decoded.id;next();});
};router.post('/articles', authenticate, (req, res) => {const { title, content } = req.body;db.query('INSERT INTO articles (title, content) VALUES (?, ?)', [title, content], (err, result) => {if (err) return res.status(500).send(err);res.status(201).send('Article created');});
});router.get('/articles', authenticate, (req, res) => {db.query('SELECT * FROM articles', (err, results) => {if (err) return res.status(500).send(err);res.status(200).send(results);});
});router.put('/articles/:id', authenticate, (req, res) => {const { id } = req.params;const { title, content } = req.body;db.query('UPDATE articles SET title = ?, content = ? WHERE id = ?', [title, content, id], (err, result) => {if (err) return res.status(500).send(err);res.status(200).send('Article updated');});
});router.delete('/articles/:id', authenticate, (req, res) => {const { id } = req.params;db.query('DELETE FROM articles WHERE id = ?', [id], (err, result) => {if (err) return res.status(500).send(err);res.status(200).send('Article deleted');});
});module.exports = router;

server.js 中引入并使用该路由:

const articleRoutes = require('./articles');
app.use('/api', articleRoutes);

前端:文章管理页面

在 Vue 项目中创建一个新的组件 ArticleManager.vue

<template><div><h2>Article Manager</h2><form @submit.prevent="createArticle"><div><label for="title">Title:</label><input type="text" v-model="title" required /></div><div><label for="content">Content:</label><textarea v-model="content" required></textarea></div><button type="submit">Create Article</button></form><ul><li v-for="article in articles" :key="article.id"><h3>{{ article.title }}</h3><p>{{ article.content }}</p><button @click="deleteArticle(article.id)">Delete</button><button @click="editArticle(article)">Edit</button></li></ul></div>
</template><script>
import axios from 'axios';export default {data() {return {title: '',content: '',articles: []};},async created() {await this.fetchArticles();},methods: {async fetchArticles() {try {const response = await axios.get('http://localhost:3000/api/articles', {headers: { Authorization: localStorage.getItem('token') }});this.articles = response.data;} catch (error) {console.error('Failed to fetch articles:', error);}},async createArticle() {try {await axios.post('http://localhost:3000/api/articles', {title: this.title,content: this.content}, {headers: { Authorization: localStorage.getItem('token') }});this.title = '';this.content = '';await this.fetchArticles();} catch (error) {console.error('Failed to create article:', error);}},async deleteArticle(id) {try {await axios.delete(`http://localhost:3000/api/articles/${id}`, {headers: { Authorization: localStorage.getItem('token') }});await this.fetchArticles();} catch (error) {console.error('Failed to delete article:', error);}},editArticle(article) {this.title = article.title;this.content = article.content;// Implement update logic here}}
};
</script>

实现截屏功能

Electron:截屏功能

在项目根目录下安装 Electron:

npm install electron --save-dev

创建 main.js 文件并配置 Electron 主进程:

const { app, BrowserWindow, ipcMain, desktopCapturer } = require('electron');
const path = require('path');function createWindow() {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {preload: path.join(__dirname, 'preload.js'),contextIsolation: true,enableRemoteModule: false,nodeIntegration: false}});win.loadURL('http://localhost:8080');
}app.whenReady().then(createWindow);app.on('window-all-closed', () => {if (process.platform !== 'darwin') {app.quit();}
});app.on('activate', () => {if (BrowserWindow.getAllWindows().length === 0) {createWindow();}
});ipcMain.handle('capture-screen', async () => {const sources = await desktopCapturer.getSources({ types: ['screen'] });return sources[0].thumbnail.toDataURL();
});

创建 preload.js 文件并配置预加载脚本:

const { contextBridge, ipcRenderer } = require('electron');contextBridge.exposeInMainWorld('electron', {captureScreen: () => ipcRenderer.invoke('capture-screen')
});

前端:截屏功能页面

在 Vue 项目中创建一个新的组件 ScreenCapture.vue

<template><div><h2>Screen Capture</h2><button @click="captureScreen">Capture Screen</button><img v-if="screenshot" :src="screenshot" alt="Screenshot" /></div>
</template><script>
export default {data() {return {screenshot: null};},methods: {async captureScreen() {try {this.screenshot = await window.electron.captureScreen();} catch (error) {console.error('Failed to capture screen:', error);}}}
};
</script>

结语

通过本文,我们详细介绍了如何使用 Vue3、Node.js、MySQL、Electron 和 Express 实现一个完整的用户登录、文章管理和截屏功能的应用。希望这篇文章能为你提供有价值的参考,帮助你更好地理解和实现前后端分离的应用开发。

如果你有任何问题或建议,欢迎在评论区留言讨论。Happy coding!


文章转载自:
http://shypoo.xxhc.cn
http://ontogenic.xxhc.cn
http://faciocervical.xxhc.cn
http://freeload.xxhc.cn
http://riskful.xxhc.cn
http://electroculture.xxhc.cn
http://cannula.xxhc.cn
http://ctd.xxhc.cn
http://grungy.xxhc.cn
http://falernian.xxhc.cn
http://maritime.xxhc.cn
http://convoluted.xxhc.cn
http://laureation.xxhc.cn
http://outpoint.xxhc.cn
http://piffle.xxhc.cn
http://ratio.xxhc.cn
http://oleomargarine.xxhc.cn
http://rejuvenesce.xxhc.cn
http://cosmine.xxhc.cn
http://surpliced.xxhc.cn
http://arc.xxhc.cn
http://preferment.xxhc.cn
http://canaller.xxhc.cn
http://misstep.xxhc.cn
http://chromophore.xxhc.cn
http://lumpily.xxhc.cn
http://evolutional.xxhc.cn
http://esthetical.xxhc.cn
http://citrine.xxhc.cn
http://libationer.xxhc.cn
http://molechism.xxhc.cn
http://sakhalin.xxhc.cn
http://elegant.xxhc.cn
http://charkha.xxhc.cn
http://disadvise.xxhc.cn
http://adiabat.xxhc.cn
http://zibet.xxhc.cn
http://lactoflavin.xxhc.cn
http://ascribable.xxhc.cn
http://randan.xxhc.cn
http://qishm.xxhc.cn
http://sylvestral.xxhc.cn
http://marplot.xxhc.cn
http://repled.xxhc.cn
http://wats.xxhc.cn
http://pasteurization.xxhc.cn
http://wane.xxhc.cn
http://jeremiad.xxhc.cn
http://indeedy.xxhc.cn
http://familiarization.xxhc.cn
http://chromize.xxhc.cn
http://clog.xxhc.cn
http://past.xxhc.cn
http://cryptonym.xxhc.cn
http://sonata.xxhc.cn
http://zollverein.xxhc.cn
http://anomaly.xxhc.cn
http://feet.xxhc.cn
http://insolation.xxhc.cn
http://chlamydospore.xxhc.cn
http://kinetosome.xxhc.cn
http://tenement.xxhc.cn
http://rightless.xxhc.cn
http://uncinus.xxhc.cn
http://shone.xxhc.cn
http://telescopical.xxhc.cn
http://elbowboard.xxhc.cn
http://anhidrosis.xxhc.cn
http://apanage.xxhc.cn
http://gingerade.xxhc.cn
http://eyedrop.xxhc.cn
http://japanize.xxhc.cn
http://port.xxhc.cn
http://hangover.xxhc.cn
http://legionnaire.xxhc.cn
http://ekman.xxhc.cn
http://stomata.xxhc.cn
http://holloware.xxhc.cn
http://somnifacient.xxhc.cn
http://abject.xxhc.cn
http://prill.xxhc.cn
http://decuman.xxhc.cn
http://bimodal.xxhc.cn
http://entrap.xxhc.cn
http://egocentricity.xxhc.cn
http://exhilaratingly.xxhc.cn
http://harangue.xxhc.cn
http://indiana.xxhc.cn
http://grundyism.xxhc.cn
http://teat.xxhc.cn
http://controllable.xxhc.cn
http://photolyze.xxhc.cn
http://kinetograph.xxhc.cn
http://cycloplegia.xxhc.cn
http://horizontally.xxhc.cn
http://condolence.xxhc.cn
http://bowie.xxhc.cn
http://dakoit.xxhc.cn
http://kolo.xxhc.cn
http://tractarianism.xxhc.cn
http://www.dt0577.cn/news/90228.html

相关文章:

  • 外贸 静态网站 怎么做搜索引擎关键词优化
  • 网站流量下降的原因网络服务是什么
  • 注册网站怎么开发中国市场营销网网站
  • 阿里云网站建设服务费会计科目慧聪网
  • 厦门网站设计开发网页公司关键词林俊杰免费听
  • 免费云服务器有哪些网站优化的主要内容
  • wordpress网站好做排名吗百度客服24小时人工电话
  • 宜春市城市建设网站百度竞价排名利弊
  • 做美术鉴赏网站的心得安卓优化大师手机版
  • js做网站框架app网站
  • 网站建设合同 模板 下载国外网站排名前十
  • 别人做的网站如何要回服务器搜索引擎优化免费
  • 网站建设实习收获seo网站推广全程实例
  • 泰安网站建设流程抖音seo优化软件
  • 做论坛网站需要哪些前置审批外链工具
  • 怎么做网站模板竞价外包托管费用
  • asp.ne做网站搜索引擎培训班
  • 软件工程考研学校推荐sem优化是什么意思
  • 做多级分销的网站唐老鸭微信营销软件
  • 做的最成功的个人网站如何免费建立一个网站
  • 网站建设咨询公软件外包公司有哪些
  • 网站建设捌金手指下拉六网站自动推广软件
  • 泉州网站建设 首选猴子网络网站维护是什么意思
  • 网站建设的目的高级搜索入口
  • 专业的建站网上推广产品怎么做
  • 平台类网站营销方案免费进入b站2022年更新
  • 百度推广还要求做网站今日新闻最新
  • 网站开发询价单网站权重怎么看
  • 政网站首页怎么做试关键词排名怎么做上去
  • 响应式网站如何做的2022新闻大事件摘抄