6

I'm totally new to Javascript and I'm wondering how to use node js to pop up a alert window in browser, after sever(Nodejs) received post message from front end? Do I need to use Ajax?

phaneven
  • 887
  • 2
  • 9
  • 10

5 Answers5

7

"after sever(Nodejs) received post message from front end?" show a pop up in the browser. This can not be done. I assume you want to show a popup in if the post request is success. Because you mention about Ajax, This is how it is done.

in your post router definition in the server do it as follows

router.post('/path', function(req, res){
   //do something
   res.jsonp({success : true})
});

something like this. finally you want to send something form the server to the client. after in the client side javascript file send the post request as follows.

$.ajax({
    url:"/url/is/here",
    method: "POST",
    data : {
        data : "what you want to send",
        put : "them here"
    },
    cache : false,
    success : function (data) {
        // data is the object that you send form the server by 
        // res.jsonp();
        // here data = {success : true}
        // validate it
        if(data['success']){
            alert("message you want to show");
        }
    },
    error : function () {
        // some error handling part
        alert("Oops! Something went wrong.");
    }
});
Viran Malaka
  • 417
  • 4
  • 10
  • this answer is good, but as I guess, this question is not asking the transaction between different origins, so using res.json might much more easier to understand than using res.jsonp – jwkoo Jan 25 '19 at 18:32
6

There is a npm module for popups known as popups. You have to install it using the command npm install popups. Then use it in the following way:

var popup = require('popups');

popup.alert({
    content: 'Hello!'
});

You can find more information here

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Aneek
  • 135
  • 1
  • 1
  • 7
  • 2
    Can you please give an usage example or some link for `popupS` module usage ? – Diksha Apr 17 '18 at 12:12
  • 3
    popups not currently working with latest version of node (v14.2.0) at time of writing. Repo has not been updated for 5 years. – Hoppo Jun 03 '20 at 14:53
2

First install the alert module: npm install alert

let alert = require('alert'); 
alert("message")
0xLogN
  • 3,289
  • 1
  • 14
  • 35
Pranav v k
  • 41
  • 5
1

Nowadays, I'd rather suggest the use of node-notifier.

It can send cross-platform native notifications in Node JS.

Besides the usual title and message options, you can also set icon, sound and use wait to wait for a user action.

And you can vary the behaviour to better mix with native OS, like through MacOS Notification Center, Windows Toaster, Windows Balloons, and Linux notify-send. It has a fallback with Growl.

Simplest case scenario:

Install:

$ npm install --save node-notifier

Import:

const notifier = require('node-notifier');

Use:

notifier.notify({
    title: 'Salutations!',
    message: 'Hey there!',
    icon: path.join(__dirname, 'icon.jpg'),
    sound: true,
    wait: true
  },

That's it.

0

This works but there's a catch it sends alert box like an application does not like javascript's alert function (that sends you in browser alert popup)

bhavin nor
  • 11
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '22 at 15:38