Coder Social home page Coder Social logo

brief-nodejs-crud-with-mysql's Introduction


| nodejs express and ejs CRUD with maysql |


Firstable go Ampps open server then go to phpmyadmin create database then create table with 3 columns id auto_increment name description then follow bellow steps:

Step 1 : open new folder then install npm init

Step 2 : Install Requred packages using NPM like this ===> ==> npm install express mysql body-parser ejs --save

Step 3 : Add follwoing code in app.js

		const path = require('path');
		const express = require('express');
		const ejs = require('ejs');
		const bodyParser = require('body-parser');
		const mysql = require('mysql');
		const app = express();

		// Server Listening
		app.listen(3000, () => {
			console.log('Server is running at port 3000');
		});

Step 4 : Create Database Connection const mysql=require('mysql');

		const connection=mysql.createConnection({
		  host:'localhost',
		  user:'root',
		  password:'put your massword',
		  database:'put name of your database)'
		});
		
		connection.connect(function(error){
		  if(!!error) console.log(error);
		  else console.log('Database Connected!');
		}); 

Setp 5 : Define view engin with ejs / public path / view files path / bodyParser/express static

		app.set('view engine', 'ejs');
                    app.set('views',path.join(__dirname,'views'));
		app.use(bodyParser.json());
		app.use(bodyParser.urlencoded({ extended: false }));
                    app.use(express.static(path.join(__dirname)));
                    app.use(express.static(path.join(__dirname, 'public')));
                    app.use(express.static(path.join(__dirname, 'public','css')));

Setp 6 : Define index path with '/' and ejs file

		//route for user index page
		app.get('/',(req, res) => {
			res.render('index', {
				title: 'ALL AUTHORS '
			});
		});

Setp 7 : Run a server and check with Browser node app

		http://localhost:3000/

Step 8 : Get value from database and show in ejs template

brief-nodejs-crud-with-mysql's People

Contributors

abdilahcoding avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.