Guest User

Untitled

a guest
Oct 26th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. const express = require('express');
  2. const mysql = require('mysql');
  3. const bodyParser = require('body-parser');
  4. const path = require('path');
  5. const cors = require('cors');
  6. const router = express.Router();
  7. const multer = require('multer');
  8. const storage = multer.diskStorage({
  9. destination: function (req, file, cb) {
  10. cb(null, './assets/images/')
  11. },
  12. filename: function (req, file, cb) {
  13. cb(null, file.originalname)
  14. }
  15. });
  16. const fileFilter = (req, file, cb)=>{
  17. if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png'){
  18. cb(null, true);
  19. }
  20. else{
  21. cb(null, false);
  22. }
  23. };
  24. upload = multer({
  25. storage: storage,
  26. limits:{
  27. filesize : 1024 * 1024 * 5
  28. },
  29. fileFilter : fileFilter
  30. });
  31.  
  32. const app = express();
  33.  
  34. //DATABASE CONNECTION
  35. const connection = mysql.createConnection({
  36. host: 'localhost',
  37. user:'root',
  38. password: 'root',
  39. database: 'inpblog',
  40. port: 8889
  41. });
  42.  
  43. // ALLOW CROSS ORIGIN
  44. const corsOptions = {
  45. origin: 'http://localhost:4200',
  46. origin1: 'http://localhost:4202',
  47. optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
  48. };
  49. app.use(cors(corsOptions));
  50. app.use('./assets/images', express.static(path.join(__dirname, 'dist', 'upload')));
  51.  
  52. const jsonParser = bodyParser.json();
  53. const urlencodedParser = bodyParser.urlencoded({ extended: false });
  54.  
  55.  
  56. connection.connect(function(error){
  57. if(!!error){
  58. console.log("error - db not connected");
  59. }
  60. else{
  61. console.log("connected");
  62. }
  63. });
  64.  
  65. // Get All Post
  66. app.get('/getallposts', (req, res) => {
  67. let sql = 'SELECT * FROM insdb';
  68. let query = connection.query(sql, (err, results) => {
  69. if(err) throw err;
  70. console.log(results);
  71. res.send(results);
  72. });
  73. });
  74.  
  75. app.get('*', (req, res) => {
  76. res.sendFile(path.join(__dirname, 'dist/index.html'));
  77. });
  78.  
  79. app.listen(4202);
Add Comment
Please, Sign In to add comment