How to transmit data to client-side js file from the server?

sniffingdoggo

I want to transmit my data to client-side JS file from the server, but right now the browser displays data on its main screen like when we use innerHTML property:

enter image description here

I have checked bunch of express.js tutorials but seems like there are no way to send (add, technically) data to the client side js file.

This is a diagram what I'm looking for:

[open the webpage] -> [(in server) get data from database] -> [send data to client-side js file] -> // do stuff more in the client-side

Any tips to resolve this problem?

This is my code:

// Makes display the client-side html, temporary disabled.
// app.use(express.static('client'));

// Queries
const QUERIES = {
  prev: 'SELECT * FROM en.prevstore',
  curr: 'SELECT * FROM en.currstore',
  next: 'SELECT * FROM en.nextstore',
  samp: 'SELECT * FROM en.sample'
}

// Create connection
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password123',
  database: 'en'
});

// Router
app.use('/', (req, res) => {
  db.query(QUERIES.samp, (err, results) => {
    let ternary = err ? console.error(err) : res.json(results);
  })
})

Client-Side HTML (request from the comment)

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="quotes">
      should be filled as quotes of data from database
    </div>
    <div class="color">
      should be filled as color of data from database
    </div>
    <script type="text/javascript" src="./index.js"></script>
  </body>
</html>

Client-Side JS:

function getWord() {
  fetch('http://localhost:4000')
  .then(res => res.json())
  .then(({data}) => console.log(data))
}
getWord() // I know it won't work but tried for just in case.
Kornflexx

When you tried to load localhost:4000 on your browser and it is requesting your / endpoint.

Your server has to return your static files (index.html & ...) on this endpoint.

app.use(express.static('public'));
// public or any static directory containing your index.html, .js, images ...

Then you can move your / endpoint to something more explicit like /quotes

app.use('/quotes', (req, res) => {
  db.query(QUERIES.samp, (err, results) => {
    if (err) {
      console.log('error', err);
      res.status(500);
      return res.end(); // closing the response /!\ IMPORTANT
    }
    res.json(results);
  })
})

On your client-side you will have something like that:

function getWord() {
  fetch('http://localhost:4000/quotes')
  .then(res => res.json())
  .then(data => console.log(data))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to append html formatted data from node js server to html file in client side?

How to send data from server-side (Express.js) to client-side (React.js)?

Server is not accepting connection from Client side to read data from a file

How to serve html in express.js that references js file from server and run it on client side?

Pass data from client side to server side

How to execute server side code with data from the client in next.js

How to send client side data to server side

How to get data from client side with odoo rest api server?

How do I send form data from the client side to the server

how to hash password on client side? how to transmit password from java device(android)?

How to fetch a .txt file from a different domain from server side javascript and get it client side

How do I use client side JavaScript to find and return data from MongoDB and a Node.js server with no framework?

How to download pdf from puppeteer using Nest js as Server Side and React in Client Side?

How do I access a variable passed in an object array from client side to server side in js/googlescript

Client side data to server side

How to import data from CSV file into Meteor collection at server side

JAVA: Using RSA and SHA-1 need to transmit data from client to server (Digital Signature)

Client Side vs Server Side When GET data from API

Send gridview data from client side to server side at once

pass data from the server side to the GAS client side

Receive data from client-side on server-side

Calling a client side function from server side in node.js

How to get data from mongodb on client side?

How to pass JSON data from server to client side and use the data in Javascript?

How to move definition of lookup path from client side to server side?

RESTful PHP: how does it work from client side to server side?

How can we change page title after we get the data from server and want to load in client side or server side component?

How to open a sidebar with data from a sheet, passing a variable from server to client-side in Google Apps Script?

How to provide json text file to client side ajax request on node.js server