Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const mysql = require('mysql');
- const bodyParser = require('body-parser');
- const path = require('path');
- const cors = require('cors');
- const router = express.Router();
- const multer = require('multer');
- const storage = multer.diskStorage({
- destination: function (req, file, cb) {
- cb(null, './assets/images/')
- },
- filename: function (req, file, cb) {
- cb(null, file.originalname)
- }
- });
- const fileFilter = (req, file, cb)=>{
- if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png'){
- cb(null, true);
- }
- else{
- cb(null, false);
- }
- };
- upload = multer({
- storage: storage,
- limits:{
- filesize : 1024 * 1024 * 5
- },
- fileFilter : fileFilter
- });
- const app = express();
- //DATABASE CONNECTION
- const connection = mysql.createConnection({
- host: 'localhost',
- user:'root',
- password: 'root',
- database: 'inpblog',
- port: 8889
- });
- // ALLOW CROSS ORIGIN
- const corsOptions = {
- origin: 'http://localhost:4200',
- origin1: 'http://localhost:4202',
- optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
- };
- app.use(cors(corsOptions));
- app.use('./assets/images', express.static(path.join(__dirname, 'dist', 'upload')));
- const jsonParser = bodyParser.json();
- const urlencodedParser = bodyParser.urlencoded({ extended: false });
- connection.connect(function(error){
- if(!!error){
- console.log("error - db not connected");
- }
- else{
- console.log("connected");
- }
- });
- // Get All Post
- app.get('/getallposts', (req, res) => {
- let sql = 'SELECT * FROM insdb';
- let query = connection.query(sql, (err, results) => {
- if(err) throw err;
- console.log(results);
- res.send(results);
- });
- });
- app.get('*', (req, res) => {
- res.sendFile(path.join(__dirname, 'dist/index.html'));
- });
- app.listen(4202);
Add Comment
Please, Sign In to add comment