Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require("express");
- var app = express();
- var router = express.Router();
- var path = __dirname + '/views/';
- var http = require('http');
- var mysql = require('mysql');
- var bodyParser = require("body-parser");
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(bodyParser.json());
- app.disable('x-powered-by');
- var connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: 'xxx',
- database: 'xxx',
- });
- router.use(function (req,res,next) {
- console.log("/" + req.method);
- next();
- });
- router.get("/",function(req,res){
- res.sendFile(path + "index.html");
- });
- var queryString = 'SELECT * FROM bets';
- router.get("/bets",function(req, res){
- showBets(res);
- res.sendFile(path + "bets.html");
- console.log('closing');
- res.end();
- });
- function showBets(res1){
- connection.query(queryString, function(err, rows, fields, res2) {
- if (err) throw err;
- else{
- for (var i in rows) {
- res1.write(rows[i].title);
- console.log(rows[i].creator);
- }
- }
- });
- }
- app.use("/",router);//säger till express att använda routes vi har definierat
- app.post('/data', function(req, res){
- var title = req.body.titleinput;
- var description = req.body.descriptioninput;
- var password = req.body.passwordinput;
- var creator = req.body.creatorinput;
- var hours = req.body.hoursinput;
- var post = {
- description : description,
- title : title,
- password : password,
- creator : creator,
- hours : hours
- };
- console.log(post);
- var query = connection.query('INSERT INTO bets SET ?', post, function(err, result) {
- if (err) {
- res.send("There was a problem adding the information to the database.");
- }
- console.log(query.sql);
- });
- });
- http.createServer(app).listen(8080, function(){
- console.log('Node server is running');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement