内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Node.js中怎么構(gòu)建一個API服務(wù)器-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Node.js中怎么構(gòu)建一個API服務(wù)器,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的德陽電信服務(wù)器托管服務(wù)

配置

創(chuàng)建一個新目錄,運(yùn)行 npm init,然后按照提示操作,把你的應(yīng)用程序命名為“notable”(或者你可能喜歡的其他名字)。

npm init

一旦完成,在你的目錄中會有一個 package.json 文件。你可以開始安裝項目所需的依賴項了。

我們將使用 Express 作為自己的框架,MongoDB 作為數(shù)據(jù)庫,還有一個名為 body-parser 的包來幫助處理 JSON 請求。

npm install --save express mongodb@2.2.16 body-parser

我還強(qiáng)烈建議將 Nodemon 安裝為 dev 依賴項。這是一個非常簡單的小包,可在文件被更改時自動重啟服務(wù)器。

如果你運(yùn)行:

npm install --save-dev nodemon

然后將以下腳本添加到 package.json

// package.json
 "scripts": {
  "dev": "nodemon server.js"
 },

完整的 package.json 應(yīng)如下所示:

// package.json
{
 "name": "notable",
 "version": "1.0.0",
 "description": "",
 "main": "server.js",
 "scripts": {
  "dev": "nodemon server.js"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
  "body-parser": "^1.15.2",
  "express": "^4.14.0",
  "mongodb": "^2.2.16"
 },
 "devDependencies": {
  "nodemon": "^1.11.0"
 }
}

現(xiàn)在,你可以創(chuàng)建 server.js 文件并構(gòu)建 API 了。

我們的服務(wù)器

首先導(dǎo)入 server.js 中的所有依賴項。

// server.js
const express    = require('express');
const MongoClient  = require('mongodb').MongoClient;
const bodyParser   = require('body-parser');
const app      = express();

我們將使用 MongoClient 與數(shù)據(jù)庫進(jìn)行交互。還會將應(yīng)用初始化為 Express 框架的實(shí)例。

最后一件事就是告訴你的程序開始監(jiān)聽請求。

你可以指定一個端口,并像這樣開始監(jiān)聽:

// server.js
const port = 8000;
app.listen(port, () => {
 console.log('We are live on ' + port);
});

現(xiàn)在,如果你運(yùn)行 npm run dev(或 node server.js,如果你沒有安裝 Nodemon 的話),應(yīng)該在終端中看到“We are live on port 8000”的提示。

你的服務(wù)器已經(jīng)啟動了。但它現(xiàn)在還什么也做不了。

接下來讓我們解決這個問題。

CRUD 路由

對于本例,你要構(gòu)建4條路由; 創(chuàng)建筆記,閱讀筆記,更新筆記和刪除筆記。

這將使你了解如何使用 Node 構(gòu)建幾乎所有的基本路由。

但是,要測試你的API,還需要模仿客戶端發(fā)出請求。為此,我們將使用名為 Postman 的優(yōu)秀應(yīng)用。它允許你使用自定義的頭和參數(shù)進(jìn)行簡單的 HTTP 請求。

安裝Postman,讓我們開始設(shè)置路由。

項目結(jié)構(gòu)

大多數(shù) Node.js 教程(以及許多真實(shí)的案例)都將所有路由放在一個很大的 routes.js 文件中。這讓我有點(diǎn)不舒服。相比之下,將文件拆到為單獨(dú)的文件夾可以提高可讀性,并使大型應(yīng)用更易于管理。

雖然我們現(xiàn)在做的不是大型應(yīng)用,但仍然可以這樣做。創(chuàng)建以下目錄:一個 app 文件夾,里面有一個routes文件夾,routes 里面有 index.jsnote_routes.js 文件。

mkdir app
cd app
mkdir routes
cd routes
touch index.js
touch note_routes.js

對于你的簡單小程序來說,這些目錄可能看起來有些過分,但從一開始就做好總是有意義的。

你的第一個路由

讓我們從 CRUD 中的 C 開始。你將會如何創(chuàng)建一個筆記?

那么,在你開始之前,必須先要打好基礎(chǔ)。在Express中,路由包含在一個函數(shù)中,該函數(shù)將 Express 實(shí)例和數(shù)據(jù)庫作為參數(shù)。

像這樣:

// routes/note_routes.js
module.exports = function(app, db) {
};

然后,你可以通過 index.js 導(dǎo)出此函數(shù):

// routes/index.js
const noteRoutes = require('./note_routes');
module.exports = function(app, db) {
 noteRoutes(app, db);
 // Other route groups could go here, in the future
};

然后導(dǎo)入它以便在 server.js 中使用:

// server.js
const express    = require('express');
const MongoClient  = require('mongodb').MongoClient;
const bodyParser   = require('body-parser');
const app      = express();
const port = 8000;
require('./app/routes')(app, {});
app.listen(port, () => {
 console.log('We are live on ' + port);
});

請注意,由于還沒有設(shè)置數(shù)據(jù)庫,因此只需傳入一個空對象。

好的,現(xiàn)在你可以制作自己的 CREATE 路由了。

語法很簡單:

// note_routes.js
module.exports = function(app, db) {
 app.post('/notes', (req, res) => {
  // You'll create your note here.
  res.send('Hello')
 });
};

當(dāng)應(yīng)用程序收到對 '/ notes' 路徑的 post 請求時,它將執(zhí)行回調(diào)內(nèi)的代碼 —— request 對象(包含請求的參數(shù)或JSON)和 response 對象。

你可以使用 Postman 將 POST 請求發(fā)送到 localhost:8000/notes 來測試。

Node.js中怎么構(gòu)建一個API服務(wù)器

你應(yīng)該得到回復(fù):'Hello'。

太好了!你創(chuàng)建了第一個真正的路由。

下一步是在你的請求中添加一些參數(shù)并在 API 中處理它們,最后添加到你的數(shù)據(jù)庫中。

請求參數(shù)

在 Postman 中,在選擇 x-www-form-urlencoded 單選按鈕后,轉(zhuǎn)到 Body 選項卡并添加一些鍵值對。

這會將編碼后的表單數(shù)據(jù)添加到你的請求中,你可以使用 API 處理該請求。

Node.js中怎么構(gòu)建一個API服務(wù)器

你可以去嘗試更多的設(shè)置項。

現(xiàn)在在你的 note_routes.js 中,讓我們輸出 body 的內(nèi)容。

// note_routes.js
module.exports = function(app, db) {
 app.post('/notes', (req, res) => {
  console.log(req.body)
  res.send('Hello')
 });
};

用 Postman 發(fā)送請求,你會看到……undefined。

不幸的是,Express 無法自行處理 URL 編碼的表單。雖然你確實(shí)安裝了這個 body-parser 包......

// server.
const express    = require('express');
const MongoClient  = require('mongodb').MongoClient;
const bodyParser   = require('body-parser');
const app      = express();
const port = 8000;
app.use(bodyParser.urlencoded({ extended: true }));
require('./app/routes')(app, {});
app.listen(port, () => {
 console.log('We are live on ' + port);
});

Now you should see the body as an object in the terminal.

現(xiàn)在你應(yīng)該將 body 視為終端中的對象。

{ title: 'My Note Title', body: 'What a great note.' }

第一個路由的最后一步:設(shè)置數(shù)據(jù)庫,然后添加數(shù)據(jù)。

最簡單方法是通過mLab 設(shè)置 Mongo 數(shù)據(jù)庫的:它是最小的而且是免費(fèi)的,設(shè)置的速度非???。

創(chuàng)建帳戶和 MongoDB 部署后,將用戶的用戶名和密碼添加到數(shù)據(jù)庫:

Node.js中怎么構(gòu)建一個API服務(wù)器

然后復(fù)制這里第二個 URL:

Node.js中怎么構(gòu)建一個API服務(wù)器

在項目根目錄的目錄配置中,創(chuàng)建一個db.js文件。

mkdir config 
cd config
touch db.js

在里面,添加剛才的URL:

module.exports = {
 url : YOUR URL HERE
};

別忘了把你的用戶名和密碼(來自數(shù)據(jù)庫用戶的密碼,而不是你的 mLab 帳戶)添加到URL中。 (如果你要將此項目提交到 Github 上,請確保包含 .gitignore 文件 像這樣,,不要與任何人分享你的密碼。)

現(xiàn)在在你的 server.js 中,可以用 MongoClient 連接到數(shù)據(jù)庫了,使用它來包裝你的應(yīng)用程序設(shè)置:

// server.js
const express    = require('express');
const MongoClient  = require('mongodb').MongoClient;
const bodyParser   = require('body-parser');
const db       = require('./config/db');
const app      = express();
const port = 8000;
app.use(bodyParser.urlencoded({ extended: true }));
MongoClient.connect(db.url, (err, database) => {
 if (err) return console.log(err)
 require('./app/routes')(app, database);
 app.listen(port, () => {
  console.log('We are live on ' + port);
 });        
})

如果你用的是最新版本的 MongoDB(3.0+),請將其修改為:

// server.js
const express    = require('express');
const MongoClient  = require('mongodb').MongoClient;
const bodyParser   = require('body-parser');
const db       = require('./config/db');
const app      = express();
const port = 8000;
app.use(bodyParser.urlencoded({ extended: true }));
MongoClient.connect(db.url, (err, database) => {
 if (err) return console.log(err)
           
 // Make sure you add the database name and not the collection name
 const database = database.db("note-api")
 require('./app/routes')(app, database);
 app.listen(port, () => {
  console.log('We are live on ' + port);
 });        
})

這是你的基礎(chǔ)架構(gòu)的最后一個設(shè)置!

添加到你的數(shù)據(jù)庫

MongoDB將數(shù)據(jù)存儲在 collections 中。在你的項目中,你希望將筆記存儲在一個名為 notes 的 collection 中。

由于將數(shù)據(jù)庫作為路徑中的 db 參數(shù)傳入,因此可以像這樣訪問它:

db.collection('notes')

創(chuàng)建筆記就像在集合上調(diào)用 insert 一樣簡單:

const note = { text: req.body.body, title: req.body.title}
 db.collection('notes').insert(note, (err, results) => {
}

插入完成后(或由于某種原因失?。?,要么返回錯誤或反回新創(chuàng)建的筆記對象。這是完整的 note_routes.js 代碼:

// note_routes.js
module.exports = function(app, db) {
 const collection = 
 app.post('/notes', (req, res) => {
  const note = { text: req.body.body, title: req.body.title };
  db.collection('notes').insert(note, (err, result) => {
   if (err) { 
    res.send({ 'error': 'An error has occurred' }); 
   } else {
    res.send(result.ops[0]);
   }
  });
 });
};

試試看!使用 Postman 發(fā)送 x-www-form-urlencoded POST 請求,在 Body 選項卡下設(shè)置 titlebody。

響應(yīng)應(yīng)如下所示:

Node.js中怎么構(gòu)建一個API服務(wù)器

如果你登錄mLab,你還應(yīng)該能夠在數(shù)據(jù)庫中看到創(chuàng)建的筆記。

READ 路由

現(xiàn)在可以稍微加快步伐。

假設(shè)你希望通過導(dǎo)航到 localhost:8000/notes/{id} 來獲取剛創(chuàng)建的筆記。這是鏈接應(yīng)該是localhost:8000/notes/585182bd42ac5b07a9755ea3。(如果你沒有得到其中筆記的 ID,可以通過檢查 mLab 或創(chuàng)建一個新的筆記)。

以下是 note_routes.js 中的內(nèi)容:

// note_routes.js
module.exports = function(app, db) {
 app.get('/notes/:id', (req, res) => {
  
 });
 app.post('/notes', (req, res) => {
  const note = { text: req.body.body, title: req.body.title };
  db.collection('notes').insert(note, (err, result) => {
   if (err) { 
    res.send({ 'error': 'An error has occurred' }); 
   } else {
    res.send(result.ops[0]);
   }
  });
 });
};

就像以前一樣,你將在數(shù)據(jù)庫 collection 中調(diào)用一個方法。在這里,它被恰當(dāng)?shù)孛麨?findOne。

// note_routes.js
module.exports = function(app, db) {
 app.get('/notes/:id', (req, res) => {
  const details = { '_id': <ID GOES HERE> };
  db.collection('notes').findOne(details, (err, item) => {
   if (err) {
    res.send({'error':'An error has occurred'});
   } else {
    res.send(item);
   }
  });
 });
app.post('/notes', (req, res) => {
  const note = { text: req.body.body, title: req.body.title };
  db.collection('notes').insert(note, (err, result) => {
   if (err) { 
    res.send({ 'error': 'An error has occurred' }); 
   } else {
    res.send(result.ops[0]);
   }
  });
 });
};

你可以通過 req.params.id 從 URL 參數(shù)中獲取 id。但是,如果你試圖將字符串插入上面的 <ID GOES HERE> 位置,它將無法正常工作。

MongoDB 不僅要求 ID 為字符串,還要求 ID 是一個對象,它們被之為 ObjectID。

別擔(dān)心,這很容易解決。這是完整的代碼:

// note_routes.js
var ObjectID = require('mongodb').ObjectID;
module.exports = function(app, db) {
 app.get('/notes/:id', (req, res) => {
  const id = req.params.id;
  const details = { '_id': new ObjectID(id) };
  db.collection('notes').findOne(details, (err, item) => {
   if (err) {
    res.send({'error':'An error has occurred'});
   } else {
    res.send(item);
   } 
  });
 });
app.post('/notes', (req, res) => {
  const note = { text: req.body.body, title: req.body.title };
  db.collection('notes').insert(note, (err, result) => {
   if (err) { 
    res.send({ 'error': 'An error has occurred' }); 
   } else {
    res.send(result.ops[0]);
   }
  });
 });
};

嘗試使用一個筆記 ID,它應(yīng)如下所示:

Node.js中怎么構(gòu)建一個API服務(wù)器

DELETE 路由

實(shí)際上刪除對象與查找對象幾乎相同。你只需用 remove 函數(shù)替換 findOne 即可。這是完整的代碼:

// note_routes.js
// ...
 app.delete('/notes/:id', (req, res) => {
  const id = req.params.id;
  const details = { '_id': new ObjectID(id) };
  db.collection('notes').remove(details, (err, item) => {
   if (err) {
    res.send({'error':'An error has occurred'});
   } else {
    res.send('Note ' + id + ' deleted!');
   } 
  });
 });
// ...

UPDATE 路由

最后一個! PUT 方法基本上是 READ 和 CREATE 的混合體。你找到該對象,然后更新它。如果剛才你刪除了數(shù)據(jù)庫中唯一的筆記,那就再創(chuàng)建一個!

代碼:

// note_routes.js
// ...
 app.put('/notes/:id', (req, res) => {
  const id = req.params.id;
  const details = { '_id': new ObjectID(id) };
  const note = { text: req.body.body, title: req.body.title };
  db.collection('notes').update(details, note, (err, result) => {
   if (err) {
     res.send({'error':'An error has occurred'});
   } else {
     res.send(note);
   } 
  });
 });
// ...

現(xiàn)在你可以更新任何筆記,如下所示:

Node.js中怎么構(gòu)建一個API服務(wù)器

關(guān)于Node.js中怎么構(gòu)建一個API服務(wù)器就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁題目:Node.js中怎么構(gòu)建一個API服務(wù)器-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.rwnh.cn/article10/gepgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站改版、網(wǎng)站維護(hù)、電子商務(wù)、網(wǎng)站收錄、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
丽江市| 淮安市| 麟游县| 淳安县| 盘锦市| 安西县| 高阳县| 绩溪县| 东辽县| 沁水县| 高台县| 奉贤区| 平和县| 南皮县| 婺源县| 砀山县| 南宁市| 信丰县| 成都市| 凉城县| 汕尾市| 页游| 大渡口区| 宁化县| 和田县| 罗城| 凯里市| 宜春市| 桃园市| 中宁县| 郎溪县| 日土县| 鄂温| 镶黄旗| 个旧市| 锦州市| 喀什市| 广水市| 乌兰浩特市| 昌吉市| 长宁县|