Troy Griffiths
Troy Griffiths

Reputation: 361

node email form success message

I want to display a success message if an email is successfully sent. I have an html form that posts to a server with a file.Is there a way I can send data from the server to the client? I would use express or ajax to handle the form submission but I can't get either of those to work with file uploading - the only way I can get my file to upload is through an html form.

Client

<form enctype="multipart/form-data" action="/email" method="post">
     <input type="text" name="name" class="form-control" id="name">
     <!--code below uploads file -->
     <input type="file" 
            style="visibility:hidden; width: 1px;" 
            id='${multipartFilePath}' class="res" name='userFile' onchange="$(this).parent().find('span').html($(this).val().replace('C:\\fakepath\\', ''))"  /> <!-- Chrome security returns 'C:\fakepath\'  -->
    <input class="btn btn-default btn-file" type="button" value="Attach File" onclick="$(this).parent().find('input[type=file]').click();"/>
    <button type="submit" class="btn btn-xl">Send Message</button>
</form>

Server

app.post('/email', function(req, res) {
    var toSend = req.files;
    res.status(204).end()
    sendEmail(req.body, toSend);//email is sent successfully
    /***Here I want to send data back to client with either a success or error message text***/
    res.send('help');
});

Basically, I need help figuring out where to accept res.send on the client side, considering I sent my post request with an html form. The email is being sent correctly, but I have no idea how to return a string of text to the client in this case.

Upvotes: 0

Views: 844

Answers (1)

Luis Delgado
Luis Delgado

Reputation: 3734

The problem is that you are sending a status().end() and then ending the function with res.send('help'). You can't execute the function end() on res and then try to send another response afterwards.

Upvotes: 1

Related Questions