M4x201 - Javascript Technologies

Seminar 11 - Fullstack Application using Node.js/Express/MySQL/Vue3 (2)

Jérôme Landré, Amine Haraoubia, Florent Libbrecht, Fabrice Meuzeret.

URCA - IUT Troyes - MMI - 2021

1. Vue3 to display data

The "assets" folder.

    <div id="content">
      <h1>Our comics are there!</h1>
      <table id="myComics">
        <thead><th>Title</th><th>Serie</th><th>Price</th><th>Author</th><th>Editor</th></thead>
      </table>
    </div>

The "comics" table.

    <script src="???/jquery-3.6.0.min.js"></script>
    <script src="???/vue3.js"></script>
<script>
    var appCode={
      data() {
        return { // the comics array is empty at the beginning
          comics: []
        }
      },
      created() { // when the Vue object is created
        var self = this // we save the object "this" in "self"
        // now, we can get the JSON data
        $.getJSON('???', function (data) {
          self.comics = data // we push the data to the comics array
        })
      }
    }
    vue1 = Vue.createApp(appCode)
    vue1.mount('#content')
  </script>
      ...
      </table>
      <p>{{ comics }}</p>
    </div>
    ...

The JSON content displayed.

The "comics" page.

2. The search page

2.a. Setting up the "searchcomics" route

SELECT * FROM bds 
INNER JOIN editeurs ON editeurs.editeur_id=bds._editeur_id 
INNER JOIN auteurs ON auteurs.auteur_id=bds._auteur_id 
WHERE bd_titre LIKE "%uder%" 
OR bd_serie LIKE "%uder%" 
OR auteur_nom LIKE "%uder%" 
OR editeur_nom LIKE "%uder%"
...
app.get('/searchcomics/:theText', (req, res) => {
    var co=mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'root', // '' (nothing) on Windows
        database: 'dutafnode',
        port: 8889 // 3306 on Windows
    })
    co.connect()
    var theText=req.params.theText // get the route parameter
    co.query('???', function (err, rows, fields) {
        if (err) throw err
        //res.end(JSON.stringify(rows))
        co.end()
        res.json(rows)
    })
})
...

Searching for "uder".

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link href="/assets/styles.css" rel="stylesheet" type="text/css" />
    <script src="/assets/jquery-3.6.0.min.js"></script>
    <script src="/assets/vue3.js"></script>
  </head>
  <body>
    <header>
      <nav>
        <a href="/">Home</a> -
        <a href="/comics">Comics</a> -
        <a href="/search">Search</a> -
        <a href="/admin">Admin.</a>
      </nav>
    </header>
    <div id="content">
      <h1>Search the comics:</h1>
      <p>Search the comics for: 
      <input type="text" name="searchText" v-model="searchText" />
        <button ???>Search!</button></p>
      <table id="myComics">
        <thead><th>Title</th><th>Serie</th><th>Price</th>
        <th>Author</th><th>Editor</th></thead>
        ???
      </table>
      <p>{{ comicsWithText }}</p>
    </div>
    <script>
      var appCode={
        data() {
          return {
            searchText: '',
            comicsWithText: []
          }
        },
        methods: {
          searchComics: function () {
            var self = this
            $.getJSON('???, function (data) {
              self.comicsWithText = data
            })
          }

        }
      }
      vue1 = Vue.createApp(appCode)
      vue1.mount('#content')
    </script>
  </body>
</html>

Searching example 1.

Searching example 2.

Searching example 3.