-1

Quick example of Node.js, Express and MySQL

Introduction

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.

Spec

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

Only Node.js

Can check if we have the environment for node to work

Source code

Link

index.js

var http = require('http');

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

Result

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.

Express

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

Install

npm install express --save

Source code

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!');
});

Result

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.

MySQL

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

Install

npm install mysql --save

Create Data

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');

Source code

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!');
});

Result

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

We successfully acquired and displayed data from MySQL.

Feeling

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.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí