0

I'm trying to perform SQL SELECT statements on a MariaDB table using Node.js, then pass the result to a web interface. I've successfully used the mySQL module to connect to the database and execute a SQL query, returning the selected rows to console.log() in the Node.js shell (Windows 10).

How can I take this output from Node.js and display it in a simple front-end interface? If my research is correct, I believe the solution involves Express and Angular, but I can't seem to figure out the specifics.

Any help or advice is greatly appreciated. Thank you!

3
  • 1
    You need some kind of web server (for example Express) and either create API get endpoint which you access using AJAX in your frontend or you can serve generated html file containing your result. If you look up documentation and examples (tutorials) of Express it should find all that you need. Commented Jun 13, 2018 at 22:37
  • @user3210641, I'd like to serve a generated html file. I'm struggling to figure out how to actually connect Express so that it can interpret the Node output. It seems to be writing JSON format to console.log(). Commented Jun 13, 2018 at 22:40
  • Please provide some code to see how far you got so far and how to point you to the right direction. Commented Jun 13, 2018 at 22:43

1 Answer 1

1

Express is probably a good place to start, but you can ignore Angular for now.

You can create a simple endpoint where browsers can see the queried data using Express.

Install Express and body-parser: npm install --save express body-parser

This is a simple example of how you can start serving up this data and view it in the browser.

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var server = app.listen(8080, function () { // create a server
    console.log("app running on port.", server.address().port);
});

app.get("/", function(req, res) { // listens for requests to localhost:8080
    res.send(data); // put your data here
});

Now when you include this code and start your server, you can navigate to localhost:8080 (the port we set in the above code) and you should see whatever data is).

If you want to start serving up pages like you are used to seeing, you should check our a templating engine like pug, but for now I think this is a good place to start.

Sign up to request clarification or add additional context in comments.

3 Comments

Where you say "// put your data here", do I insert all of the JS including the connection information and SQL query?
No, you can think of res.send() like console.log() but for spitting stuff out to the browser when someone visits a page. You can try res.send("test"), just like how you can do console.log("test"). What I meant by that is that you said you were printing the data to console, but you wanted it to be shown on a webpage, so instead of doing console.log(your_variable), use res.send(your_variable) and visit the page.
Great! Now your next step is probably looking into creating views and using a templating engine. Have fun!

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.