Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
How to POST javascript code in express instead of using script tag in html?

How can I make this work without using a script tag inside html. This is the current code, it works, but I want the code to come from express post request.

EDIT: If I run as script inside html, author, title, text, and date post correctly. If the code is inside express, everything posts except for date.

 //html
    <form action="/blogs" method="post">
        <input type="hidden" id="date" name="date"/> 
        <script>
          const event = new Date(Date.now());
          const options = {  year: 'numeric', month: 'long', day: 'numeric' };
          const today = event.toLocaleDateString('en-US', options)
          document.getElementById('date').value = today;
        </script>
    </form>

//express
app.post('/blogs', (req, res) => {
    const { author, title, text, date } = req.body;
    blogPosts.push({ author, title, text, date, })
    res.send("New Post Added!")
})

This is what I tried...

//express
app.post('/blogs', (req, res) => {
    const event = new Date(Date.now());
    const options = {  year: 'numeric', month: 'long', day: 'numeric' };
    const today = event.toLocaleDateString('en-US', options)
    const { author, title, text, date } = req.body;
    blogPosts.push({ author, title, text, date, today })
    res.send("New Post Added!")
})

Update: The final working code. Thanks Chris G!

app.post('/blogs', (req, res) => {
  const event = new Date(Date.now());
  const options = {  year: 'numeric', month: 'long', day: 
  'numeric' };
  const date = event.toLocaleDateString('en-US', options)
  const { author, title, text } = req.body;
  blogPosts.push({ author, title, text, date })
  res.send("New Post Added!")
})

I know there are a lot of pros that will look at this and say "idiot", which is fair but please understand that I'm just starting out in programming and I really love doing this! I look up to all the experts on here and envy your talent. I want to learn, not asking for a freebie or someone to do my work. Just an explanation or someone to point me in the right direction would be more than enough. Thank you for your time!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda