12/08/2018, 16:07

Quick example of Node.js, Express and MySQL

Regarding node, it illustrates the JS execution environment to install various nomads to use at the front at npm, but I tried to make it because it is possible to create a web server as well. Front end engineer node, npm are available for front-end use Little knowledge of infrastructure and ...

Regarding node, it illustrates the JS execution environment to install various nomads to use at the front at npm, but I tried to make it because it is possible to create a web server as well.

  • Front end engineer
  • node, npm are available for front-end use
  • Little knowledge of infrastructure and servers
  • Little knowledge of MySQL

Can check if we have the environment for node to work

Link

index.js

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World
');
}).listen(1234);

Run on node index.js and access localhost: 1234.

What I'm doing is just returning a character string, but it's quite amazing to work as a web server alone.

I confirmed that we can create a web server with node.js.

In other words, there are a lot of frameworks to easily create web servers.

Express is one of such framework.

Public link

npm install express --save

Link index.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Run in node index.js , access to localhost:3000

It just return a string, but it was easy to make.

Truly a framework. Even if I do not know, I can still read the source code somehow.

node.js can help you to display the data of the web server from the DB

If you install mysql via npm, you can operate MySQL from JS.

※ I did surprise that we have to handle the MySQL with JS

npm install mysql --save

All is mysql after login

create database NodeTest;

use NodeTest;

create table test_table(id int(11),name varchar(255));

insert into test_table values(1,'testname');

Link index.js

var express = require('express');
var app = express();

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'NodeTest'
});

app.get('/', function (req, res) {
  connection.query('select * from test_table', function (error, results, fields) {
    if (error) throw error;
    res.send(results[0]);
  });
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Execute with node index.js and access localhost: 3000.

We successfully acquired and displayed data from MySQL.

It was easy to create a web server, and it was easy to get data from MySQL.

If it is a simple personal service or the like, it is likely that the range that can be created with this will increase at once.

This time I just returned a string, but next time I will try to return HTML.

0