Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu

express.js adding a router with the regular expression

Posted on 8 June, 2019 by admin

You can create a route with express that will handle a few pages like portfolio, projects, reviews, using such a structure as shows below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require('express');
const router = express.Router();
 
const DB = require('../../core/my_db/DB.js')
 
 
 
// # 1
/* /api/v1/users/:login/portfolio/?user&portfolio */
router.get(/\/(.*)\/(portfolio|projects|reviews)/, function(req, res, next) {
  let login = req.params[0]
  
  const getUser = (login) => {
    return new Promise((resolve, reject) => {
      DB.pool.query(
        `SELECT *
         FROM `users`
         WHERE login = ?`,
        [login],
        (err, r) => {
          if (err) throw err
          if (r.length === 1) {
            delete r[0].pass
            return resolve({ user: r[0] })
          }
          return reject({ error: "notFound" })
        }
      )
    })
  }
 
  const getPortfolio = (login) => {
    return new Promise((resolve, reject) => {
      DB.pool.query(
        `SELECT p.*
         FROM `users` u
         INNER JOIN `portfolios` p
         ON p.userId = u.id AND u.login = ?`,
        [login],
        (err, r) => {
          if (err) throw err
          resolve({ portfolios: r })
        }
      )
    })
  }
 
  const getProjects = (login) => {
    return new Promise((resolve, reject) => {
      DB.pool.query(
        `SELECT p.*
        FROM `users` u
        INNER JOIN `projects` p
        ON p.userId = u.id AND u.login = ?`,
        [login],
        (err, r) => {
          if (err) throw err
          resolve({ projects: r })
        }
      )
    })
  }
 
  const getReviews = (login) => {
    return new Promise((resolve, reject) => {
      DB.pool.query(
        `SELECT r.*,
            u2.id as u2_id,
            u2.login,
            u2.avatar,
            u2.firstName,
            u2.secondName
        FROM `users` u
        INNER JOIN `reviews` r
        ON r.userId = u.id AND u.login = ?
        INNER JOIN `users` u2
        ON r.authorId = u2.id`,
        [login],
        (err, r) => {
          if (err) throw err
          resolve({ reviews: r })
        }
      )
    })
  }
 
  
 
  let requests = []
  console.log('req.query = ', req.query);
 
  if (req.query.hasOwnProperty('user')) {
    requests.push(getUser(login))
  }
 
  if (req.query.hasOwnProperty('portfolio')) {
    requests.push(getPortfolio(login))
  }
 
  if (req.query.hasOwnProperty('projects')) {
    requests.push(getProjects(login))
  }
 
  if (req.query.hasOwnProperty('reviews')) {
    requests.push(getReviews(login))
  }
  
  
  
 
  
 
  Promise.all(requests)
    .then(values => {
      console.log(values); // [3, 1337, "foo"]
      res.json(values)
    })
    .catch(err => {
      res.json(err)
    })
  
  
});
 
 
 
// # 2
/* /api/v1/users/:login/projects/ */
router.get('/:login/projects/', function(req, res, next) {
  res.json({ hello:'world' })
})
 
module.exports = router;

 

It’s very important the line that specifies the regex should be a js regular expression. Not a string!

1
router.get(/\/(.*)\/(portfolio|projects|reviews)/, function(req, res, next) {

 

 

the end

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant