Just learning nodejs. I want to change content inside my html without completely rendering in the page when I post.
Here is my current code.
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
var arr = [];
app.get('/', function (req, res) {
res.render('index', {messages: null, error: null});
})
app.post('/user', function (req, res) {
var input = req.body.userInput;
var output = "";
arr.push(input);
for(var i = 0; i < arr.length;i++){
(function(){
console.log(arr[i]);
output +=arr[i] + "</br>";
})(i);
}
res.render('index', {messages: output, error: null});
})
app.listen(port, function () {
console.log('Example app listening on port 3000!')
})
Thanks. Any advice is really appreciated.
lazy loading?res.render(), send just the data in json likeres.json({messages: output, error: null}). Make it a REST API. Then use the json in the front-end to update the page without reloading the full page.