Basic configuration for runing node.js and mysql

i use xamp

1)first create database shavedb

2)create tables (as specify in queries)

3)in xamp the default access to phpmyadmin username: root password: empty

4)copy the code to server.js and put the file in folder for your app, before runing this file by node server.js
run :
npm install express

npm install mysql

//take from here
//https://expressjs.com/en/guide/routing.html
const express = require(‘express’);
const app = express();
var mysql = require(‘mysql’);
var con = mysql.createConnection({
host: ‘localhost’,
database: ‘shavedb’,
user: ‘root’,
password: ”
});
con.connect(function(err) {
if (err) throw err;
console.log(‘Connected!’);
// con.query(‘CREATE DATABASE shavedb’, function(err, result) {
// if (err) throw err;
// console.log(‘Database created’);
// });
// var sql = ‘RENAME TABLE usersgroups TO users_groups’;
// con.query(sql, function(err, result) {
// if (err) throw err;
// console.log(‘1 record inserted’);
// });
});
// GET method route
app.get(‘/’, function(req, res) {
res.send(‘GET request to the homepage’);
});
// app.get(‘/users’, function(req, res) {
// con.query(‘SELECT * FROM users’, function(err, result) {
// if (err) throw err;
// res.send(result);
// });
// });
app.get(‘/groups/:groupId’, function(req, res) {
con.query(
‘SELECT * FROM groups where id = ‘ + con.escape(req.params.groupId),
function(err, result) {
if (err) throw err;
res.send(result);
}
);
});
//—————————————-start Example—————————–
//get query&params in express
//etc. example.com/user/000000?sex=female
/*app.get(‘/user/:id’, function(req, res) {
const query = req.query; // query = {sex:”female”}
const params = req.params; //params = {id:”000000″}
});*/
//—————————————-end Example—————————–
// POST method route
app.post(‘/’, function(req, res) {
res.send(‘POST request to the homepage’);
});
app.listen(3000, () => console.log(‘Example app listening on port 3000!’));
Advertisements
This entry was posted in כללי. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s