Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var express = require('express');
- var app= express();
- var bodyParser = require('body-parser');
- app.use (bodyParser.json());
- app.use (bodyParser.urlencoded({ extended: true }));
- var server = app.listen(process.env.PORT||8000, function(err){
- if (err) throw err;
- console.log('Connected to port 8000');
- });
- var conn = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: 'rootpass',
- database: 'mydb'
- });
- conn.connect(function(err){
- if(err) throw err;
- console.log ('Connected');
- });
- conn.query('CREATE TABLE IF NOT EXISTS student\
- (\
- id INT,\
- nume VARCHAR(30),\
- prenume VARCHAR(30),\
- facultate VARCHAR(30),\
- oras VARCHAR(30)\
- )', function(err, res){
- if (err) throw err;
- console.log(res);
- });
- app.get('/insert.html', function(req, res){
- res.sendFile(__dirname + '/insert.html');
- });
- app.post('/insert', function(req, res){
- var arr = [[req.body.id, req.body.nume, req.body.prenume, req.body.facultate, req.body.oras]];
- conn.query('INSERT INTO student VALUES ?', [arr], function(err, result){
- if(err) throw err;
- console.log('succes');
- });
- res.end();
- });
- conn.query('SELECT * FROM student', function(err, res){
- if(err) throw err;
- console.log(res);
- });
- app.get('/select',function(req, res) {
- conn.query ('SELECT * FROM student', function(err, result) {
- if(err) throw err;
- res.send(result);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement