I would like connect the function of file home.html to file index.js. It's possible?
const express = require('express');
const app = express();
const Port = 8080;
app.use(express.json());
app.listen(
Port,
() => console.log('its alive on http://localhost:' + Port)
);
var url = [];
var User = [];
app.post('/tshirt/:id', (req, res) => {
if (req.body.Request == "NovaMusica") {
res.send({
Musica: req.body.Musica, User: req.body.User
});
url.push(req.body.Musica);
User.push(req.body.User);
var testa = require('./home.html');
testa.test(url, User)
}});
home.html:
<html><head>
<script>
export function test(... args){
$.each(url, function(index, value) {
$('<label style="margin-top:15px;display:inline-block;margin-left: 5%;">'+url[index]+' (time)<br><br>   '+User[index]+'</label>', {
'text': value
}).appendTo('sendMusics');
});
}
I would like connect function test of home.html to request on app.post.
It's possible? Thanks for helping me
Note: You cannot run client-side, DOM-manipulating JS on the server; it requires a browser with your page loaded to run.
Nonetheless, here's what you'd need to do in more useful cases:
Move the script from the HTML to a file test.js:
// test.js
function test(... args){
$.each(url, function(index, value) {
$('<label style="margin-top:15px;display:inline-block;margin-left: 5%;">'+url[index]+' (time)<br><br>   '+User[index]+'</label>', {
'text': value
}).appendTo('sendMusics');
});
}
export { test };
Then inside home.html replace the inline script by
<script src="./path/to/test.js" type="module"></script>
So inside your index.js you can then import it (at the top of your file):
import { test as testa } from './path/to/test.js';