第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Building RESTful APIs with Express(Node.js)

標簽:
Node.js

REST defines a set of conventions for creating HTTP services:

  • POST: to create a resource
  • PUT: to update it
  • GET: to read it
  • DELETE: to delete it

Express is a simple, minimalistic and lightweight framework for building web
servers.

// Build a web server
const express = require(‘express’);
const app = express();

// Creating a course
app.post(/api/courses’, (req, res) => {
// Create the course and return the course object
resn.send(course);
});

// Getting all the courses
app.get(/api/courses’, (req, res) => {
// To read query string parameters (?sortBy=name)
const sortBy = req.query.sortBy;

// Return the courses
res.send(courses);
});

// Getting a single course
app.get(/api/courses/:id’, (req, res) => {
const courseId = req.params.id;

// Lookup the course
// If not found, return 404
res.status(404).send(‘Course not found.);

// Else, return the course object
res.send(course);
});

// Updating a course
app.put(/api/courses/:id’, (req, res) => {
// If course not found, return 404, otherwise update it
// and return the updated object.
});

// Deleting a course
app.delete(/api/courses/:id’, (req, res) => {
// If course not found, return 404, otherwise delete it
// and return the deleted object.
});

// Listen on port 3000
app.listen(3000, () => console.log(‘Listening…’));
  • We use Nodemon to watch for changes in files and automatically restart the
    node process.
  • We can use environment variables to store various settings for an application. To
    read an environment variable, we use process.env.
// Reading the port from an environment variable
const port = process.env.PORT || 3000;
app.listen(port);
  • You should never trust data sent by the client. Always validate! Use Joi package
    to perform input validation.

  • A middleware function is a function that takes a request object and either
    terminates the request/response cycle or passes control to another middleware
    function.

  • Express has a few built-in middleware functions:

  • json(): to parse the body of requests with a JSON payload

  • urlencoded(): to parse the body of requests with URL-encoded payload

  • static(): to serve static files

  • You can create custom middleware for cross-cutting concerns, such as logging,
    authentication, etc.

// Custom middleware (applied on all routes)
app.use(function(req, res, next)) {
// …
next();
}
// Custom middleware (applied on routes starting with /api/admin)
app.use(/api/admin’, function(req, res, next)) {
// …
next();
}
  • We can detect the environment in which our Node application is running
    (development, production, etc) using process.env.NODE_ENV and
    app.get(‘env’).
  • The config package gives us an elegant way to store configuration settings for
    our applications.
  • We can use the debug package to add debugging information to an application.
    Prefer this approach to console.log() statements.
  • To return HTML markup to the client, use a templating engine. There are various
    templating engines available out there. Pug, EJS and Mustache are the most
    popular ones.
點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消