最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js+Express+Vue+MySQL+axios的項(xiàng)目搭建全過程

 更新時間:2022年12月07日 09:58:42   作者:鍵.  
這篇文章主要介紹了Node.js+Express+Vue+MySQL+axios的項(xiàng)目搭建全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1 基本搭建

創(chuàng)建vue項(xiàng)目之前需要先安裝Node.js和MySQL數(shù)據(jù)庫

1.1 vue腳手架安裝

npm i vue -g
npm i @vue/cli -g

//初始化vue項(xiàng)目
vue create 項(xiàng)目名

1.2 在創(chuàng)建好的項(xiàng)目中創(chuàng)建server文件夾

在文件夾內(nèi)創(chuàng)建這幾個文件:

新建api文件夾、db.js、index.js、sqlMap.js。(api文件存放相關(guān)api接口路徑及方法,db.js配置相關(guān)數(shù)據(jù)庫,index.js配置后端端口及api路由)

index.js

const userApi = require('./api/userApi');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

// 后端api路由
app.use('/api/user', userApi);

// 監(jiān)聽端口
app.listen(3000);
console.log('success listen at port:3000......');

db.js

//數(shù)據(jù)庫配置信息 并進(jìn)行導(dǎo)出
module.exports ={
  mysql:{
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'h5',
    port: '3306'
  }
}

sqlMap.js

var sqlMap = {
  // 用戶
  user: {
    add: 'insert into user(name,age) values(?,?)',
	update:'....'
  },
  //商品
  goods:{
	  add:'insert into goods(name,desc,price,num) value(?,?,?,?)'
  },
  //訂單
  orders:{
	  ....
  }
}
module.exports = sqlMap;

userApi.js

//引入express,調(diào)用路由模塊
var express = require('express');
var router = express.Router();

//引入mysql,mysql配置文件
var models = require('../db');
var mysql = require('mysql');
var $sql = require('../sqlMap');

// 連接數(shù)據(jù)庫
var conn = mysql.createConnection(models.mysql);

conn.connect();
var jsonWrite = function(res, ret) {
  if(typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失敗'
    });
  } else {
    res.json(ret);
  }
};

// 增加用戶接口  完整進(jìn)入該post的路徑問index.js里面配置的路由加上該post的路徑
//   '/api/user/addUser'
router.post('/addUser', (req, res) => {
  let sql = $sql.user.add;
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.name, params.pass], function(err, result) {
    if (err) {
      console.log("添加失敗"+err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});

module.exports = router;

1.3 編寫前端界面

在src的views目錄下建一個前端界面

<template>
  <div class="home">
	<form action="http://localhost:3000/api/user/addUser" method="post">
		user:<input type="text" name="name" /><br>
		pass:<input type="password" name="pass" /><br>
		<input type="submit"/>
	</form>
	
	<button type="button" @click="addUser">post添加用戶</button>
  </div>
</template>

<script>

export default {
  name: 'Home',
  methods:{
	  addUser(){
		// console.log(111)
		//發(fā)起ajax請求
		//使用axios請求
	  }
  }
}
</script>

1.4 啟動服務(wù)測試

此時我們還沒有安裝mysql模塊

npm i mysql --save

同時啟動前端和后端的服務(wù)

輸入數(shù)據(jù)測試:

數(shù)據(jù)插入成功!

這里需要注意一點(diǎn):

前端界面表單中的name要和數(shù)據(jù)庫中的字段一致,不然數(shù)據(jù)不能插入數(shù)據(jù)庫而顯示為NULL

2 axios的使用

axios安裝和使用

安裝axios

npm i --save axios

在vue項(xiàng)目中的main.js中引入axios

// 引入axios
import axios from 'axios'

//全局注冊axios
Vue.prototype.$axios=axios
<template>
  <div class="home">
	<form action="http://localhost:3000/api/user/addUser" method="post">
		user:<input type="text" name="name" /><br>
		pass:<input type="password" name="pass" /><br>
		<input type="submit"/>
	</form>
	
	<button type="button" @click="selectUser">post添加用戶</button>
  </div>
</template>

<script>

export default {
  name: 'Home',
  methods:{
	  selectUser(){
		// console.log(111)
		//發(fā)起ajax請求
		//使用axios請求
		// console.log(this.$axios)
		this.$axios({
			url:'http://localhost:3000/api/user/',//請求的url地址
			method:'post',
			data:{
				name:'胡桃',
			},
		}).then(function(data){
			console.log(data)
		}).catch(function(err){
			console.log(err)
		})
	  }
  }
}
</script>

測試報(bào)錯,因?yàn)镹ode后端和vue端端口不一致,需要跨域

配置proxy進(jìn)行跨域請求

vue根目錄找到vue.config.js,如果沒有就手動創(chuàng)建一個

module.exports={
	devServer:{
		proxy:{//配置跨域
			'/api':{
				target:'http://localhost:3000/',//這里填寫真實(shí)的后臺接口
				changeOrigin:true,//允許跨域
				pathRewrite:{
				/* 重寫路徑,當(dāng)我們在瀏覽器中看到請求的地址為:http://localhost:8080/api/core/getData/userInfo 時
		            實(shí)際上訪問的地址是:http://121.121.67.254:8185/core/getData/userInfo,因?yàn)橹貙懥?/api
		           */
					'^/api':'/'
				}
			}
		}
	}
}

userApi.js中新增一個post

router.post('/selectUser', (req, res) => {
  console.log(req.body);
});
<template>
  <div class="home">
	<form action="http://localhost:3000/api/user/addUser" method="post">
		user:<input type="text" name="name" /><br>
		pass:<input type="password" name="pass" /><br>
		<input type="submit"/>
	</form>
	
	<button type="button" @click="selectUser">post添加用戶</button>
  </div>
</template>

<script>

export default {
  name: 'Home',
  methods:{
	  selectUser(){
		// console.log(111)
		//發(fā)起ajax請求
		//使用axios請求
		// console.log(this.$axios)
		this.$axios({
			url:'/api/api/user/selectUser',//請求的url地址
			method:'post',
			data:{
				name:'胡桃',
			},
		}).then(function(data){
			console.log(data)
		}).catch(function(err){
			console.log(err)
		})
	  }
  }
}
</script>

重啟前端和后端服務(wù),點(diǎn)擊按鈕

查看后臺控制臺

跨域請求數(shù)據(jù)成功!

proxy寫多個代理

module.exports={
	devServer:{
		proxy:{//配置跨域
			'/api/select':{
				target:'http://localhost:3000/',//這里填寫真實(shí)的后臺接口
				changeOrigin:true,//允許跨域
				pathRewrite:{
					'^/api/select':''
				}
			},
			'/api':{
				target:'http://localhost:3000/',//這里填寫真實(shí)的后臺接口
				changeOrigin:true,//允許跨域
				pathRewrite:{
				  // api代表網(wǎng)址是: http://localhost:3000/api/user
					'^/api':''
				}
			},
		}
	}
}

前端url路徑寫成哪個都可以跨域請求到數(shù)據(jù)

代碼更改后需要充錢前端服務(wù)

簡寫代碼:

同理,我們把第二個代理修改

但是這里測試請求失敗,

我們做如下修改:

最終就可以測試成功了!

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • node.js中的http.response.write方法使用說明

    node.js中的http.response.write方法使用說明

    這篇文章主要介紹了node.js中的http.response.write方法使用說明,本文介紹了http.response.write的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • NodeJS創(chuàng)建最簡單的HTTP服務(wù)器

    NodeJS創(chuàng)建最簡單的HTTP服務(wù)器

    這篇文章主要介紹了NodeJS創(chuàng)建最簡單的HTTP服務(wù)器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Node.js EventEmmitter事件監(jiān)聽器用法實(shí)例分析

    Node.js EventEmmitter事件監(jiān)聽器用法實(shí)例分析

    這篇文章主要介紹了Node.js EventEmmitter事件監(jiān)聽器用法,結(jié)合實(shí)例形式分析了EventEmmitter事件監(jiān)聽器相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下
    2019-01-01
  • 在Nodejs中實(shí)現(xiàn)一個緩存系統(tǒng)的方法詳解

    在Nodejs中實(shí)現(xiàn)一個緩存系統(tǒng)的方法詳解

    在數(shù)據(jù)庫查詢遇到瓶頸時,我們通??梢圆捎镁彺鎭硖嵘樵兯俣?同時緩解數(shù)據(jù)庫壓力,在一些簡單場景中,我們也可以自己實(shí)現(xiàn)一個緩存系統(tǒng),避免使用額外的緩存中間件,這篇文章將帶你一步步實(shí)現(xiàn)一個完善的緩存系統(tǒng),需要的朋友可以參考下
    2024-03-03
  • nodejs 圖解express+supervisor+ejs的用法(推薦)

    nodejs 圖解express+supervisor+ejs的用法(推薦)

    下面小編就為大家?guī)硪黄猲odejs 圖解express+supervisor+ejs的用法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • nodejs更改項(xiàng)目端口號的方法

    nodejs更改項(xiàng)目端口號的方法

    今天小編就為大家分享一篇nodejs更改項(xiàng)目端口號的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • node.js開發(fā)中使用Node Supervisor實(shí)現(xiàn)監(jiān)測文件修改并自動重啟應(yīng)用

    node.js開發(fā)中使用Node Supervisor實(shí)現(xiàn)監(jiān)測文件修改并自動重啟應(yīng)用

    這篇文章主要介紹了node.js開發(fā)中使用Node Supervisor實(shí)現(xiàn)監(jiān)測文件修改并自動重啟應(yīng)用的功能,從而避免大量重復(fù)的CTRL+C終止程序動作,需要的朋友可以參考下
    2014-11-11
  • Express框架之connect-flash詳解

    Express框架之connect-flash詳解

    本篇文章主要介紹了Express框架之connect-flash詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 深入淺出了解Node.js Streams

    深入淺出了解Node.js Streams

    這篇文章講了了解流的用途,為什么它們?nèi)绱酥匾约叭绾问褂盟鼈?。下面我們來一起學(xué)習(xí)吧
    2019-05-05
  • Node.js中的緩沖與流模塊詳細(xì)介紹

    Node.js中的緩沖與流模塊詳細(xì)介紹

    這篇文章主要介紹了Node.js中的緩沖與流模塊詳細(xì)介紹,本文講解了緩沖(buffer)模塊、類:Buffer、寫入緩沖區(qū)、復(fù)制緩沖區(qū)、流模塊等內(nèi)容,需要的朋友可以參考下
    2015-02-02

最新評論

民乐县| 龙泉市| 石门县| 伊金霍洛旗| 临颍县| 阳东县| 维西| 盱眙县| 乐陵市| 三门县| 扎兰屯市| 桦川县| 汕头市| 平阳县| 新巴尔虎左旗| 武威市| 贺兰县| 彝良县| 南丰县| 宿松县| 葫芦岛市| 天津市| 桂平市| 仙游县| 甘南县| 行唐县| 辉县市| 北票市| 玉林市| 清苑县| 鄄城县| 淮安市| 广元市| 鄂尔多斯市| 娱乐| 滨州市| 稻城县| 乐陵市| 什邡市| 五峰| 仙桃市|