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

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

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

配置

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

npm init

一旦完成,在你的目錄中會(huì)有一個(gè) package.json 文件。你可以開始安裝項(xiàng)目所需的依賴項(xiàng)了。

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

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

我還強(qiáng)烈建議將 Nodemon 安裝為 dev 依賴項(xiàng)。這是一個(gè)非常簡單的小包,可在文件被更改時(shí)自動(dòng)重啟服務(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 中的所有依賴項(xiàng)。

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

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

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

你可以指定一個(gè)端口,并像這樣開始監(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)啟動(dòng)了。但它現(xiàn)在還什么也做不了。

接下來讓我們解決這個(gè)問題。

CRUD 路由

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

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

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

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

項(xiàng)目結(jié)構(gòu)

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

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

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

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

你的第一個(gè)路由

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

那么,在你開始之前,必須先要打好基礎(chǔ)。在Express中,路由包含在一個(gè)函數(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);
});

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

好的,現(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)用程序收到對(duì) '/ notes' 路徑的 post 請(qǐng)求時(shí),它將執(zhí)行回調(diào)內(nèi)的代碼 —— request 對(duì)象(包含請(qǐng)求的參數(shù)或JSON)和 response 對(duì)象。

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

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

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

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

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

請(qǐng)求參數(shù)

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

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

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

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

現(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ā)送請(qǐng)求,你會(huì)看到……undefined。

不幸的是,Express 無法自行處理 URL 編碼的表單。雖然你確實(shí)安裝了這個(gè) 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 視為終端中的對(duì)象。

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

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

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

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

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

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

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

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

mkdir config 
cd config
touch db.js

在里面,添加剛才的URL:

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

別忘了把你的用戶名和密碼(來自數(shù)據(jù)庫用戶的密碼,而不是你的 mLab 帳戶)添加到URL中。 (如果你要將此項(xiàng)目提交到 Github 上,請(qǐng)確保包含 .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+),請(qǐng)將其修改為:

// 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)的最后一個(gè)設(shè)置!

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

MongoDB將數(shù)據(jù)存儲(chǔ)在 collections 中。在你的項(xiàng)目中,你希望將筆記存儲(chǔ)在一個(gè)名為 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) => {
}

插入完成后(或由于某種原因失?。?,要么返回錯(cuò)誤或反回新創(chuàng)建的筆記對(duì)象。這是完整的 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 請(qǐng)求,在 Body 選項(xiàng)卡下設(shè)置 titlebody。

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

Node.js中怎么構(gòu)建一個(gè)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)建一個(gè)新的筆記)。

以下是 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)用一個(gè)方法。在這里,它被恰當(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 是一個(gè)對(duì)象,它們被之為 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]);
   }
  });
 });
};

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

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

DELETE 路由

實(shí)際上刪除對(duì)象與查找對(duì)象幾乎相同。你只需用 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 路由

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

代碼:

// 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)建一個(gè)API服務(wù)器

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

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

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

廣告

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

外貿(mào)網(wǎng)站建設(shè)