node+vue+MongoDB从构建项目到服务器部署
来源:互联网 发布:宝贝上淘宝首页 编辑:程序博客网 时间:2024/05/20 06:57
用到技术:node-vue-vuex-axios-es6/es7-mongodb
记录一下项目从无到有到服务器部署
Centos 6
MongoDB 3.2.7
一、利用vue-cli构建项目:
1、安装vue-cli至全局
npm install -g vue-cli
2、使用vue-cli初始化项目:
vue init webpack-simple node-vue-demo
3.安装依赖:
npm install
4.运行项目:
npm run dev
二、启动node服务及安装mongodb
1.在项目文件夹下创建server文件夹放node代码
2.在server文件夹下创建app.js
const api = require('./api')const fs = require('fs')const path = require('path')const bodyParser = require('body-parser')const express = require('express')const app = express()app.use(bodyParser.json());app.use(bodyParser.urlencoded({extended: false}));app.use(api);// 静态文件app.use(express.static(path.resolve(__dirname, '../dist')))app.get('*', function(req, res) { const html = fs.readFileSync(path.resolve(__dirname, '../dist/index.html'), 'utf-8') res.send(html)})//允许跨域访问app.all('*',function (req, res, next) { console.log(req,res) res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header('Access-Control-Allow-Headers', 'Content-Type'); next();});// 监听80端口app.listen(80);console.log('server is running 80');
3.创建api.js
"use strict";const models = require('./db');const express = require('express');const router = express.Router();// 创建账号router.post('/api/login/createAccount',(req,res) => { let newAccount = new models.Login({ account : req.body.account, password : req.body.password }); newAccount.save((err,data) => { if (err) { res.send(err); } else { res.send('Create success'); } });});// 获取全部账号router.get('/api/login/getAccount',(req,res) => { // 查找数据库 models.Login.find((err,data) => { if (err) { res.send(err); } else { res.send(data); } });});module.exports = router;
5.安装下载并安装mongodb(linux安装方法)
这里我们在官网下载源码进行安装. 下载地址: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgz使用wget下载cd /usr/localwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgztar -xvf mongodb-linux-x86_64-rhel62-3.2.7.tgzmv mongodb-linux-x86_64-rhel62-3.2.7 mongodb配置环境变量修改/etc/profile, 添加如下内容export MONGODB_HOME=/usr/local/mongodbexport PATH=$MONGODB_HOME/bin:$PATH执行命令:vim /etc/profilesource /etc/profile查看mongodb版本信息 mongod -v启动创建数据库目录MongoDB需要自建数据库文件夹.mkdir -p /data/mongodbmkdir -p /data/mongodb/logtouch /data/mongodb/log/mongodb.log添加配置文件新建mongodb.conf配置文件, 通过这个配置文件进行启动.vim /etc/mongodb.conf配置文件内容:dbpath=/data/mongodblogpath=/data/mongodb/log/mongodb.loglogappend=trueport=27017fork=true##auth = true # 先关闭, 创建好用户在启动通过配置文件启动mongod -f /etc/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 2814child process started successfully, parent exiting出现successfully表示启动成功了.进入 MongoDB后台管理 Shellcd /usr/local/mongodb/bin./mongo创建数据库use testswitched to db test创建成功.创建用户, 设置权限db.createUser( { user: "test", pwd: "test", roles: [ { role: "readWrite", db: "test" } ] })
4.连接mongodb
const mongoose = require('mongoose');// 连接数据库mongoose.connect("mongodb://admin:Password@172.0.0.1:27017/admin",{useMongoClient:true});//如没有创建用户则:"mongodb://172.0.0.1:27017/testconst db = mongoose.connection;db.once('error',() => console.log('Mongo connection error'));db.once('open',() => console.log('Mongo connection successed'));const loginSchema = mongoose.Schema({ account : String, password : String});const Models = { Login : mongoose.model('Login',loginSchema)}module.exports = Models;
5.启动node服务
node /server/app.js
6.vue调用node服务
<template> <div class="hello"> <img src="../assets/logo.png"> <h2>Hello</h2> <div> <p>账号:<input type="text" v-model='account'></p> <p>密码:<input type="text" v-model='password'></p> <button @click='login'>创建</button> <button @click='query'>查看创建</button> </div> <ul> <li style="display:block;" v-for="(val, key) in list"> <span>用户名:{{val.account}}密码:{{val.password}}</span> </li> </ul> </div></template><script>import http from "./../lib/http";import apiUrl from "./../lib/ApiSetting";export default { name: 'hello', data () { return { msg: 'Welcome to Your Vue.js App', account:'', password:'', list:{} } }, created() { }, methods: { login(){ if(this.account&&this.password){ http(apiUrl.createAccount, { account:this.account, password:this.password }) .then((res)=>{ alert('创建成功') }) } }, query(){ http(apiUrl.getAccount) .then((res)=>{ console.log(res.data) this.list = res.data }) } }}</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped>h1, h2 { font-weight: normal;}ul { list-style-type: none; padding: 0;}li { display: inline-block; margin: 0 10px;}a { color: #42b983;}</style>
三、node服务常驻linux系统
$ sudo npm install forever -g #安装$ forever start ./server/app.js #启动$ forever stop ./server/app.js #停止$ forever start -l forever.log -o out.log -e err.log ./server/app.js #输出日志和错误//此次有个小坑,如果进入server里面运行app.js会提示未找到模块
部署完成
GitHub地址:https://github.com/474782977/node-vue
阅读全文
2 0
- node+vue+MongoDB从构建项目到服务器部署
- Vue+Node+MongoDB从开发到正式部署
- Vue+Node+MongoDB从开发到正式部署
- 一步步教你怎么把vue项目部署到Node服务器上
- Vue项目webpack打包部署到服务器
- vue项目 构建部署
- Vue+Node+MongoDB搭建部署发布
- 将项目从 GitHub 部署到服务器
- 从nvm安装到vue-cli构建项目
- 【Web前端】Vue.js项目部署到服务器
- vue项目部署到apache服务器页面空白的问题
- 配置maven构建的webApp项目部署到Tomcat服务器
- 从安装node js到构建一个vue并启动它
- node部署到linux服务器
- 使用Vue.js 2.0搭建单页应用:从构建到部署
- 部署项目到服务器
- 部署项目到服务器
- 项目部署到服务器
- 统计学习方法笔记:支持向量机之线性可分支持向量机与硬间间隔最大化
- Java实现栈Stack和队列Queue
- idea gulp打包
- 练习一 打印下列华氏温度与摄氏温度对照表
- 笔记--servlet基础复习2
- node+vue+MongoDB从构建项目到服务器部署
- PHP 正则表达式
- wireshark 捕捉过滤器和显示过滤器的区别 及各自的用法
- 依赖注入(DI)
- WOJ1402-Build Your Home
- linux中一些网络方面的基础知识
- 京东笔试 输出最短字符串
- Nginx服务器搭建和基本配置详解_nginx
- 深度学习利器:TensorFlow与NLP模型