0

I am expanding Node.js and using them alongside HTML files.

However, I am unable to figure the following.

I build a form.html where variables such as firstName, lastName can be gathered. The formed is filled, and the bottom is pressed, and it executes an app.js with an action = "/signup".

The action is followed by the following app.js containing:

// Bodyparser Middleware
app.use(bodyParser.urlencoded({ extended: true }));

// Static folder
app.use(express.static(path.join(__dirname, 'public')));

//signup route
app.post('/signup', (req, res){
//Processes including storing data into mailchimp and mongodb
});

Now after the process is performed a new HTML file will be open, success.html.

In success.html I wish to create a custom message with some of the variables gathered from the previous processes from form.html.

<p>Sample text <h1><%= name %></h1> sample text</p>

I have tried solutions suggested here and here and other options. I even tried changing to action = "file.php" and then executing two .php files with different <form action=""> and it has not work.

1 Answer 1

0

I can see that you're using EJS notation in your success.html. In that case you can just res.render the file.

You can see here how can you use EJS with Express. But once you created the success.ejs file (most probably in your case changing the extension will do)

Then you can just do instead of res.sendFile:

res.render('success', {
        name: firstName + " " + lastName
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you and I will have a look into it. However, is there another way to achieve it? I am trying to execute everything within app.post('/signup', (req, res){});
you will execute this inside app.post('/signup', (req, res){... }); instead of res.send or res.json you are using res.render which is the standard way to render a view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.