xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js RESTful API & EJS template engine All In One

Node.js RESTful API & EJS template engine All In One

模版引擎

  1. stream
  2. buffer

RESTful API

demos

https://developer.github.com/v3/

//jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/wikiDB", {
  useNewUrlParser: true
});

const articleSchema = {
  title: String,
  content: String
};

const Article = mongoose.model("Article", articleSchema);

/////////////////////////All Articles///////////////////////////////////

app.route("/articles")


.get(function(req, res){
  Article.find(function(err, articles){
    if (articles) {
      const jsonArticles = JSON.stringify(articles);
      res.send(jsonArticles);
    } else {
      res.send("No articles currently in wikiDB.");
    }
  });
})

.post(function(req, res){
  const newArticle = Article({
    title: req.body.title,
    content: req.body.content
  });

  newArticle.save(function(err){
    if (!err){
      res.send("Successfully added a new article.");
    } else {
      res.send(err);
    }
  });
})

.delete(function(req, res){

  Article.deleteMany(function(err){
    if (!err){
      res.send("Successfully deleted all the articles in wikiDB.");
    } else {
      res.send(err);
    }
  });

});


/////////////////////////Individual Articles///////////////////////////////////

app.route("/articles/:articleTitle")

.get(function(req, res){
  const articleTitle = req.params.articleTitle;
  Article.findOne({title: articleTitle}, function(err, article){
    if (article){
      const jsonArticle = JSON.stringify(article);
      res.send(jsonArticle);
    } else {
      res.send("No article with that title found.");
    }
  });
})

.patch(function(req, res){
  const articleTitle = req.params.articleTitle;
  Article.update(
    {title: articleTitle},
    {content: req.body.newContent},
    function(err){
      if (!err){
        res.send("Successfully updated selected article.");
      } else {
        res.send(err);
      }
    });
})

.put(function(req, res){

  const articleTitle = req.params.articleTitle;

  Article.update(
    {title: articleTitle},
    {content: req.body.newContent},
    {overwrite: true},
    function(err){
      if (!err){
        res.send("Successfully updated the content of the selected article.");
      } else {
        res.send(err);
      }
    });
})


.delete(function(req, res){
  const articleTitle = req.params.articleTitle;
  LostPet.findOneAndDelete({title: articleTitle}, function(err){
    if (!err){
      res.send("Successfully deleted selected article.");
    } else {
      res.send(err);
    }
  });
});


app.listen(3000, function() {
  console.log("Server started on port 3000");
});

free API

https://github.com/public-apis/public-apis

EJS 模版引擎

https://www.npmjs.com/package/ejs

https://github.com/mde/ejs

if 条件逻辑判断

<% if (user) { %>
  <h2><%= user.name %></h2>
<% } %>

ejs-express-server.js

const express = require('express')
const app = express()

// const router = require('./routes/article')
// app.use('articles', router)

// views
app.set('view engine', 'ejs')

// app.get('/articles', function(req, res) {
//   res.render('articles/index');
// });

// http://localhost:3000/
app.get('/', (req, res) => {
  // mock data
  const articles = [{
    title: 'Test Article',
    image: '<img src="https://cdn.xgqfrms.xyz/logo/logo.png" class="card-img-top" />',
    url: 'https://cdn.xgqfrms.xyz/logo/logo.png'
  }];
  // views
  res.render('articles/index', { articles: articles });
})

const port = 3000;
app.listen(port);
console.log(`app is running on http://localhost:${port}/`)

http://localhost:3000/

views/articles/index.ejs

<meta charset="UTF-8">
<title>EJS Is Fun</title>

<body>
  <div>
    <% articles.forEach(function(article) { %>
      <div>
        <h2><%= article.title %></h2>
        image <%- article.image %> <br/>
        <img class="card-img-top" src="<%= article.url %>" /> <br/>
      </div>
    <% }); %>
    </div>
</body>

image

https://www.digitalocean.com/community/tutorials/how-to-use-ejs-to-template-your-node-application

demos

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://stackoverflow.com/questions/77072206/how-do-i-add-ejs-to-a-src-object-of-an-image/77073264#77073264



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-04-21 22:34  xgqfrms  阅读(267)  评论(13编辑  收藏  举报