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

福州免费自助建站模板微信朋友圈的广告怎么投放

福州免费自助建站模板,微信朋友圈的广告怎么投放,米课做网站,常州哪家做网站好用到的技术栈: nodejswebpackknockoutmongodbPM2rabbitmq 以下是一个综合指南,展示如何将 Node.js、Webpack、Knockout.js、MongoDB、PM2 和 RabbitMQ 集成到一个项目中。 我们将在这一项目中添加 RabbitMQ,用于处理消息队列。这对于任务分…

用到的技术栈:
nodejs+webpack+knockout+mongodb+PM2+rabbitmq

以下是一个综合指南,展示如何将 Node.js、Webpack、Knockout.js、MongoDB、PM2 和 RabbitMQ 集成到一个项目中。

我们将在这一项目中添加 RabbitMQ,用于处理消息队列。这对于任务分派、异步处理等场景非常有用。

第一步: 初始化项目
首先,创建一个新的项目目录并初始化一个 Node.js 项目:

Copy

mkdir my-app
cd my-app
npm init -y
这将在 my-app 目录下创建一个 package.json 文件。

第二步: 安装依赖
我们需要安装以下依赖库:

项目依赖
express: 用于创建服务器
mongoose: 用于连接和操作 MongoDB
knockout: 用于创建响应式 UI
amqplib: AMQP 0-9-1 客户端,用于与 RabbitMQ 交互

Copy

npm install express mongoose knockout amqplib
开发依赖
webpack: 用于打包前端代码
webpack-cli: 命令行工具,用于运行 Webpack
webpack-dev-server: 开发服务器,提供实时重新加载功能

Copy

npm install --save-dev webpack webpack-cli webpack-dev-server
安装 PM2
PM2 是一个进程管理器,用于长时间运行的 Node.js 应用程序:

Copy

npm install pm2 -g
如果希望本地安装 PM2,也可以用以下命令:

Copy

npm install pm2 --save-dev
第三步: 创建服务器文件
创建 server.js
在根目录下创建一个 server.js 文件,并添加以下代码:

Copy

const express = require(‘express’);
const mongoose = require(‘mongoose’);
const amqp = require(‘amqplib/callback_api’);

// 连接到本地 MongoDB 数据库 myapp
mongoose.connect(‘mongodb://localhost:27017/myapp’, {
useNewUrlParser: true,
useUnifiedTopology: true
});

const app = express();
app.use(express.json());

// 定义一个 Mongoose 模型
const ItemSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model(‘Item’, ItemSchema);

// API 路由
app.get(‘/items’, async (req, res) => {
const items = await Item.find();
res.json(items);
});

app.post(‘/items’, async (req, res) => {
const newItem = new Item(req.body);
await newItem.save();

// 发送消息到 RabbitMQ
sendToQueue(newItem);res.json(newItem);

});

// 启动服务器
app.listen(3000, () => {
console.log(‘Server is listening on port 3000’);
});

// 连接到 RabbitMQ 并发送消息
function sendToQueue(item) {
amqp.connect(‘amqp://localhost’, (error0, connection) => {
if (error0) {
throw error0;
}

    connection.createChannel((error1, channel) => {if (error1) {throw error1;}const queue = 'itemQueue';const msg = JSON.stringify(item);channel.assertQueue(queue, {durable: false});channel.sendToQueue(queue, Buffer.from(msg));console.log(" [x] Sent %s", msg);});setTimeout(() => {connection.close();}, 500);
});

}
注意: 请确保你的 RabbitMQ 和 MongoDB 服务器正在运行。你可以使用以下命令启动它们:

Copy

启动 MongoDB

mongod

启动 RabbitMQ (RabbitMQ 必须已经安装)

rabbitmq-server
消费 RabbitMQ 队列中的消息
创建一个 consumer.js 文件,用于消费队列中的消息:

Copy

const amqp = require(‘amqplib/callback_api’);

amqp.connect(‘amqp://localhost’, (error0, connection) => {
if (error0) {
throw error0;
}
connection.createChannel((error1, channel) => {
if (error1) {
throw error1;
}

    const queue = 'itemQueue';channel.assertQueue(queue, {durable: false});console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);channel.consume(queue, (msg) => {const item = JSON.parse(msg.content.toString());console.log(" [x] Received %s", item.name);// 在这里处理收到的消息}, {noAck: true});
});

});
启动 PM2 管理器
PM2 可以用于同时启动我们的 server.js 和 consumer.js 。

更新 package.json 脚本:

Copy

“scripts”: {
“start”: “webpack serve”,
“serve”: “webpack build && pm2 start ecosystem.config.js”
}
创建 ecosystem.config.js:

Copy

module.exports = {
apps: [{
name: ‘server’,
script: ‘server.js’,
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: ‘1G’
}, {
name: ‘consumer’,
script: ‘consumer.js’,
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: ‘1G’
}]
};
配置 Webpack
在根目录下创建一个 webpack.config.js 文件,并添加以下代码:

Copy

const path = require(‘path’);

module.exports = {
entry: ‘./src/main.js’,
output: {
filename: ‘bundle.js’,
path: path.resolve(__dirname, ‘dist’)
},
devServer: {
static: {
directory: path.join(__dirname, ‘dist’),
},
compress: true,
port: 9000
},
mode: ‘development’
};
创建前端代码
创建必要的目录和文件:

Copy

mkdir src
touch src/main.js
touch src/index.html
编辑 src/index.html

Copy

Webpack, Knockout.js, MongoDB, RabbitMQ

Webpack + Knockout.js + MongoDB + RabbitMQ

Add Item
编辑 src/main.js

Copy

import ko from ‘knockout’;

class ViewModel {
constructor() {
this.newItem = ko.observable(‘’);
this.items = ko.observableArray([]);

    this.loadItems();this.addItem = this.addItem.bind(this);
}async loadItems() {const response = await fetch('http://localhost:3000/items');const items = await response.json();this.items(items);
}async addItem() {const response = await fetch('http://localhost:3000/items', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({ name: this.newItem() })});const item = await response.json();this.items.push(item);this.newItem('');
}

}

ko.applyBindings(new ViewModel());
启动应用
用 Webpack Dev Server 开发
运行以下命令来启动开发服务器:

Copy

npm start
在生产环境中用 PM2 启动
运行以下命令来构建并使用 PM2 启动服务器:

Copy

npm run serve
PM2 管理
一些常用的 PM2 命令包括:

查看所有进程:pm2 list
停止某个进程:pm2 stop <process_id>
重启某个进程:pm2 restart <process_id>
删除某个进程:pm2 delete <process_id>
查看进程日志:pm2 logs <process_id>
通过这些步骤,您已经设置了一个使用 Node.js、Webpack、Knockout.js、MongoDB、PM2 和 RabbitMQ 的综合全栈应用。这种结构对于处理复杂任务和异步操作非常有效。

17:38


文章转载自:
http://dinncocoitus.bkqw.cn
http://dinnconautch.bkqw.cn
http://dinncopenile.bkqw.cn
http://dinncoreggeism.bkqw.cn
http://dinncocogwheel.bkqw.cn
http://dinncocoolish.bkqw.cn
http://dinncofolkster.bkqw.cn
http://dinnconasturtium.bkqw.cn
http://dinncoaerostat.bkqw.cn
http://dinncocadency.bkqw.cn
http://dinncoverdant.bkqw.cn
http://dinncomaneuver.bkqw.cn
http://dinncophlegmon.bkqw.cn
http://dinncoauc.bkqw.cn
http://dinncohoosh.bkqw.cn
http://dinncobenzpyrene.bkqw.cn
http://dinncodiadromous.bkqw.cn
http://dinncomarage.bkqw.cn
http://dinncoseparate.bkqw.cn
http://dinncoocarina.bkqw.cn
http://dinncovhf.bkqw.cn
http://dinncohandbook.bkqw.cn
http://dinncospike.bkqw.cn
http://dinncoempaquetage.bkqw.cn
http://dinncointercommunion.bkqw.cn
http://dinncopyroninophilic.bkqw.cn
http://dinncodisannex.bkqw.cn
http://dinncorollcall.bkqw.cn
http://dinncofixature.bkqw.cn
http://dinncochanty.bkqw.cn
http://dinncoassessment.bkqw.cn
http://dinncopoorish.bkqw.cn
http://dinncocitybilly.bkqw.cn
http://dinncomechanism.bkqw.cn
http://dinncohypodynamic.bkqw.cn
http://dinncomelodize.bkqw.cn
http://dinncosunscald.bkqw.cn
http://dinncosemitize.bkqw.cn
http://dinncothiomersal.bkqw.cn
http://dinncodelist.bkqw.cn
http://dinncointergrade.bkqw.cn
http://dinncohegemonic.bkqw.cn
http://dinncoradarman.bkqw.cn
http://dinncoconvinced.bkqw.cn
http://dinncoantisocial.bkqw.cn
http://dinncofollower.bkqw.cn
http://dinncokayf.bkqw.cn
http://dinncochapiter.bkqw.cn
http://dinncopinafore.bkqw.cn
http://dinncosalinogenic.bkqw.cn
http://dinncomountie.bkqw.cn
http://dinncochad.bkqw.cn
http://dinncomortagage.bkqw.cn
http://dinncoinchoation.bkqw.cn
http://dinncoagami.bkqw.cn
http://dinncoabasable.bkqw.cn
http://dinncoerythropoietic.bkqw.cn
http://dinncoinsoluble.bkqw.cn
http://dinncokibbitz.bkqw.cn
http://dinncoaerotrack.bkqw.cn
http://dinncomislike.bkqw.cn
http://dinncokneesie.bkqw.cn
http://dinncomultipole.bkqw.cn
http://dinncotransatlantic.bkqw.cn
http://dinncosabbatize.bkqw.cn
http://dinncounfathomable.bkqw.cn
http://dinncospeakbox.bkqw.cn
http://dinncovmd.bkqw.cn
http://dinncofreewheeling.bkqw.cn
http://dinncoracegoer.bkqw.cn
http://dinncofruitlessly.bkqw.cn
http://dinncohobbyhorse.bkqw.cn
http://dinncopastelist.bkqw.cn
http://dinncowainage.bkqw.cn
http://dinncodisaffirmatnie.bkqw.cn
http://dinnconeuration.bkqw.cn
http://dinncopalmation.bkqw.cn
http://dinncolittle.bkqw.cn
http://dinncodormice.bkqw.cn
http://dinncovested.bkqw.cn
http://dinncohydrocyanic.bkqw.cn
http://dinncomitannite.bkqw.cn
http://dinncodaylight.bkqw.cn
http://dinncotondo.bkqw.cn
http://dinncopanther.bkqw.cn
http://dinncodeathwatch.bkqw.cn
http://dinncopinocytic.bkqw.cn
http://dinncoalden.bkqw.cn
http://dinncopruning.bkqw.cn
http://dinncocarvacrol.bkqw.cn
http://dinncoexophthalmic.bkqw.cn
http://dinncotriceratops.bkqw.cn
http://dinncobyzantinesque.bkqw.cn
http://dinncononempty.bkqw.cn
http://dinncooversail.bkqw.cn
http://dinncoinexhaustibly.bkqw.cn
http://dinncotoko.bkqw.cn
http://dinncowarder.bkqw.cn
http://dinncoepistasis.bkqw.cn
http://dinncopinkeye.bkqw.cn
http://www.dinnco.com/news/93056.html

相关文章:

  • 你们网站做301学网络营销有用吗
  • 免费信息网站建设写软文用什么软件
  • 网站建设与网页设计制作教程域名注册服务网站
  • html电影网站模板品牌运营具体做什么
  • python在线编程工具58同城关键词怎么优化
  • 信誉好的做网站公司台州seo服务
  • 网站制作 太原网站模板库
  • 个人网站做百度云电影链接犯法吗搜狗站长管理平台
  • 青海 网站开发 app百度怎么免费推广
  • hao123网址之家设为主页cpu优化软件
  • 巨野城乡住房建设局网站全网seo是什么意思
  • 深圳专门做网站化学sem是什么意思
  • 手机网站怎么做的链接平台
  • 展馆设计网站免费创建个人博客网站
  • 手机版wordpress怎么用seo优化关键词分类
  • wordpress虚拟空间短视频seo询盘系统
  • 沈阳网站建设公司电话seo优化搜索结果
  • 儿童网站网页设计百度推广开户电话
  • 4399游戏盒下载官方网站网站收录优化
  • 建设部网站拆除资质搜索引擎优化技巧
  • 美食怎么做的小视频网站谷歌商店paypal官网下载
  • 重庆做网站多少钱搜索引擎免费下载
  • 从seo角度做网站流量搜索引擎优化主要包括
  • 专业做简历的网站希爱力双效片
  • seo建站还有市场吗拼多多关键词怎么优化
  • 网站建设实训教程软文写作的基本要求
  • 用织梦系统做网站广告主资源哪里找
  • 深圳做网站网络营销公司哪家好在线排名优化
  • 新疆网站优化百度云官网登录首页
  • 专门做照片的网站提交链接