Quantcast
Channel: User JohnP - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by JohnP for Getting Data from Node.js file and displaying it in HTML/JS page

$
0
0

You should start by setting up a server to serve requests. I use expressjs for this - http://expressjs.com/

This will allow you to run nodejs as a web application.

Setup a route in express JS to serve your data - http://expressjs.com/api.html#express

var express = require('express');var app = express();app.get('/data', function(req, res){  res.send('hello world'); //replace with your data here});app.listen(3000);

Open up a browser, and type in http://MY_SERVER_ADDR:3000/data and you should see your output there.

Next, you'll need to attach an event handler to your HTML file that will trigger a $.get() request when it is triggered. Add the previous url to your data in your $.get call and do something with it.

$('.my_selector').click(function(){   $.get('http://MY_SERVER_ADDR:3000/data', {}, function(data){        console.log(data)   });});

That should get you going.


Viewing all articles
Browse latest Browse all 37

Trending Articles