Managing Events in EJS

I am currently utilizing the combination of Node.js, Express.js, and Ejs.

Challenge

In my particular scenario, I have 3 distinct layers:

1) clients-side.js ((node.js)

2) server.js (node.js)

3) front-end.ejs (ejs)

Upon successful form submission, an event is emitted in client-side.js:

let eventEmitter = new events.EventEmitter();

eventEmitter.emit('added');

`and this event is captured in server.js using express, ejs, body-parser within the app.post() method:

eventEmitter.on('added', function(){ console.log("added successfully") });

Additionally

eventEmitter.on('added', addEvent);

var addEvent= function () {

console.log('added successfully...');

Is it possible to introduce a variable here and pass it to ejs/html for handling?

If so, how can I determine within ejs/html when the event occurs?

}

The message is currently displayed in the console.

Question

I aim to display the message following the added event in an alert (snackbar) contained in ejs/html (utilizing bootstrap).

I prefer not to employ AJAX methods in ejs/html and keep my two JS files separate.

Thank you in advance.

Answer №1

If you want to include an additional event within a given event and then capture it in ejs/html, you can achieve this by adding the new event and passing the value using a comma separator! Here's an example:

eventEmitter.on('added', addEvent);
var addEvent= function () {
var yourVariable ="your value";    
 eventEmitter.emit('event', yourVariable);
}`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message in Linux for NodeJS global NPM package: "File or directory does not exist"

After setting up my Ubuntu 14.04 system, I installed nodejs and npm using the command: sudo apt-get install nodejs npm To ensure packages utilize the node interpreter instead of nodejs, I created a symlink with: sudo ln -s /usr/bin/nodejs /usr/local/bin ...

Guide on using ExpressJS and RethinkDb: Dealing with the error message "Cannot read property 'prototype' of undefined"

Just starting out with databases and JavaScript, I decided to dive into learning ExpressJS and RethinkDB using a tutorial provided by this link: https://github.com/rethinkdb/rethinkdb-example-nodejs-chat Following the instructions closely, I encountered ...

Is Exposing Database Passwords on Github a Potential Security Concern?

I recently set up an express server and I'm still fairly new to the world of databases. An interesting dilemma has come up - if my express server's code is hosted publicly on Github, and it contains this snippet: const pool = new Pool({ user: ...

Validating a string with Hapi Joi validation for singles

Looking to perform basic validation on a single string, specifically the request header X-Value. Here's what I have tried. The issue I'm facing is that it always returns 'success' even when there is no X-Value header present. const ...

Modifying the value of a variable causes a ripple effect on the value of another variable that had been linked to it

After running the code below, I am receiving values from MongoDB in the 'docs' variable: collection.find({"Stories._id":ObjectID(storyId)}, {"Stories.$":1}, function (e, docs) { var results = docs; results[0].Stories = []; } I ...

Contrasting "npm run dev" with "npm start"

I have just started learning about Node and AngularJS. Could someone explain the distinction between npm run dev and npm start commands in the node terminal? ...

What makes this code run synchronously?

function foo(cb) { if (!someAuditCondition) { return cb(new Error(...)); // <- Despite this part being synchronous, the rest of the function is asynchronous. } doSomeAsynAction(function (err, data) { if (err) { return cb(err); } cb( ...

Troubleshooting issues with my node.js API integration with MongoDB Atlas using Mongoose

IMPORTANT UPDATE : After investigating, I've realized that the issue is not with my code but rather with my work network which seems to be blocking the connection. I had to use my phone's data connection to figure this out. I will look into reso ...

Using asynchronous functions in a loop in Node.js

Although this question may have been asked before, I am struggling to understand how things work and that is why I am starting a new thread. con.query(sql,[req.params.quizId],(err,rows,fields)=>{ //rows contains questions if(err) throw err; ...

Creating dynamic databases in real-time with Node, EJS, and Firebase

As I venture into learning Node JS, I am currently transitioning from a client-side chat application to a server-side one. Previously, when my app was solely client-side, any modifications to the database automatically triggered data reload in a div, provi ...

The server is sending unprocessed raw JSON data that Angular is unable to handle

I've been working on accessing data from the server and storing it in $scope.resp, but I'm only seeing raw JSON displayed on the page. Here is the code from routes/addr.js: router.get('/', function(req, res) { var persons = [{add ...

Gulp: optimizing task creation and mastering series and parallel execution #2732

I've encountered a strange issue with Gulp that I can't seem to figure out. Despite my attempts to understand and utilize it properly, I keep running into roadblocks when trying to work with multiple environments and templates in my application. ...

What are some ways to implement Node.js within Netsuite?

Recently, I've been exploring Netsuite and I'm curious to learn about the feasibility of integrating Node.js or npm modules into a SuiteScript or Suitelet. Is it possible? I have a specific aim in mind - to utilize some npm modules within Netsui ...

Applying class to target specific screen size in Bootstrap

I've been utilizing bootstrap for my project. My objective is to assign multiple classes based on screen size, such as sm-col and xs.col. For example, let's say I have a text id <div id="text" class="xs-format lg-format ..."></div> ...

Encountering a problem with the node.js version while trying to install

Currently in the process of setting up my Raspberry Pi as a node.js server. Recently performed a clean installation of Raspberian Subsequently, I installed node Upon running node -v, the system displays: v0.10.25 Proceeded to install npm When checking ...

Unable to establish a connection between nodejs, socket.io, and expressjs

I have an organization of files as follows: socket - project - main.js -node_modules - node_modules_folder_here - text.html The content of my main.js file is: var app = require('express')(); var http = require('http') ...

Tips for submitting an e-mail through an HTML form with the help of Node.js and Gulp

Today, I've been tackling the challenge of creating an HTML contact form that can send an email using Node.js and Gulp. However, I'm struggling to find a solution. Using a relative path to a simple .PHP file doesn't seem to be working, so it ...

"An unexpected compilation error (800A03EA) has occurred in the JavaScript syntax during

My computer is facing a frustrating compilation issue with JScript and node. Despite trying various solutions found online, the problem persists. After navigating to CD>PROJECT: npm init successful npm install -g globally and locally installed correct ...

Issues with Material-UI rendering in deployed applications

Struggling with Rendering Issues in my React App I am facing challenges while building my react-app, specifically when using material-ui/core version 4.10.2. While everything works perfectly on the normal react-scripts dev server, I encounter rendering pr ...

Verifying if the completion of the busboy event has already happened or not

In the form I have, there is a file processing task that takes some time to complete. Currently, while node is processing the files, if it encounters the finish event, it immediately triggers it. How can I ensure that the finish event is only fired after a ...