Home > OS >  How to append client-side scripts to page in Node
How to append client-side scripts to page in Node

Time:01-29

I'm starting project ts, node, express. Is there a way to add typescript files to html/ejs that run on client side (so i have access to document e.t.c., just like in regular js script)?

So it would be something like:

  • Node server with express that returns html pages and handles users sign in/up requests.
  • Adding, deleting, changing elements in html on user's activity handles in scripts on client side.

I would appreciate pointing on what I misunderstood in whole node, express conception if I did.

CodePudding user response:

Please refer to this link for detailed explaination: Serving html from express

You can basically serve html pages from express. On root request handler you can server index.html

app.get('/', (req, res)=>{res.render('index.html');}

Now in the index.html page have a link to /index route example:

<a href="/login">

Now, on server code have a route handler

app.get('/login', (req, res)=>{res.render('login.html');}

login.html can now have a form with action

<form action="/signin" method="post">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="login">
</form>

Now, on server code have a route handler

app.post('signin', (req, res)=>{//handle logic here});
  •  Tags:  
  • Related