短信驗證碼_02.搭建服務器環境


Node 搭建服務器環境

1.在項目下新建一個文件夾 sms-send,並用編輯器打開

2.用終端進入 sms-send 文件夾路徑

2.1.自動創建 package.json

  • 命令:npm init --yes 創建好之后就這個樣子:


2.2. 裝對應的模塊

  • npm install express body-parser request querystring --save


3.創建index.js文件

package.json 里默認入口是 index.js 。趁裝模塊的時間,我們可以先去創建 index.js

4.下載安裝 postman

參考postman官網

5.在 sms-send 文件夾終端安裝 nodemon

  • npm install nodemon -g

6.index.js代碼

//1.引入模塊
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const querystring = require('querystring');

//2.實例化app,監聽對應服務
const app = express();

//3.中間件拿過來以后,能正常獲取前端傳遞的數據
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

//4.設置對應接口,可以訪問對應地址
app.get('/',(req,res) => {
    res.send('測試使用的');
});

//5.設置端口號
const port = process.env.PORT || 5000; //沒有服務器,暫時使用5000

//6.監聽端口號是否有被訪問,被訪問的話,走入一個箭頭函數
app.listen(port,() => {
    console.log(`server is running on port ${port}`);
})


6.1.關於拿到 body-parser

7.運行

  • 在sms-send文件夾終端:nodemon
  • 沒有裝nodemon:node index.js

8.來瀏覽器測試

這樣就完成了基本的服務器環境搭建


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM