Skip to content

Commit 070be85

Browse files
committed
updated crud files
1 parent 56c404d commit 070be85

File tree

14 files changed

+925
-1
lines changed

14 files changed

+925
-1
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
# nodejs-postgresql-crud
1+
# [NodeJs & PostgreSQL Crud Application](https://github.com/dannibla/nodejs-postgresql-crud)
2+
3+
Learn how to create a Simple Crud Application by Connecting PostgreSQL with NodeJs, While you are using PostgreSQL as database and NodeJs as backend, you need PostgreSQL database packages to connect with nodejs. There are various packages available but most popular and well documented is node-postgres [pg](https://node-postgres.com/). Let’s start.
4+
5+
## What's needed
6+
7+
- Make sure you have [postgresql](https://www.postgresql.org/download/) installed on machine and [pgAdmin](https://www.pgadmin.org/download/) - postgresql management tool
8+
- Make sure you have [node.js](https://nodejs.org/en/download/) installed
9+
10+
## Folder Structure
11+
12+
Within the download you'll find the following directories and files:
13+
```
14+
Connecting NodeJs & PostgreSQL
15+
.
16+
├── LICENSE
17+
├── README.md
18+
├── public
19+
│   ├── css
20+
│   │   └── main.css
21+
│   └── js
22+
│     └── main.js
23+
├── routes
24+
│ ├── customers.js
25+
│ └── index.js
26+
├── views
27+
│   ├── customer
28+
│ │ ├── list.ejs
29+
│ │ ├── add.ejs
30+
│   │   └── edit.ejs
31+
│   ├── layouts
32+
│ │ ├── header.ejs
33+
│   │   └── footer.ejs
34+
│   └── inedx.ejs
35+
├── app.js
36+
└── package.json
37+
```
38+
39+
## Getting started
40+
41+
- Download the project’s zip
42+
- Create table in PostgreSQL
43+
44+
```
45+
CREATE TABLE customer
46+
(
47+
id serial NOT NULL,
48+
name character varying(200) NOT NULL,
49+
address text NOT NULL,
50+
email character varying(200) NOT NULL,
51+
phone character varying(20) NOT NULL
52+
)
53+
54+
```
55+
56+
- Type `npm install` in terminal/console in the source folder where `package.json` is located
57+
- Type `node app.js` in terminal/console in the source folder where `app.js` is located
58+
- server started on port 4000. (http://localhost:4000/) in default browser
59+
60+
## Help on Executing Queries
61+
62+
Documented is available node-postgres(Doc) [pg](https://node-postgres.com/features/queries)
63+
64+
## Copyright and License
65+
Copyright 2019 NodeJs & PostgreSQL Crud Application, released under the MIT License.

app.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const express = require('express');
2+
const bodyParser = require("body-parser");
3+
const path = require('path');
4+
5+
var customers = require('./routes/customers');
6+
var routes = require('./routes');
7+
var app = express();
8+
9+
app.set('port', process.env.PORT || 4000);
10+
app.set('views', path.join(__dirname, 'views'));
11+
app.set('view engine', 'ejs');
12+
13+
app.use(bodyParser.json());
14+
app.use(bodyParser.urlencoded({ extended: true }));
15+
16+
app.use(express.static(path.join(__dirname, 'public')));
17+
18+
app.get('/', routes.index);
19+
app.get('/customers', customers.list);
20+
app.get('/customers/add', customers.add);
21+
app.post('/customers/add', customers.save);
22+
app.get('/customers/delete/:id', customers.delete);
23+
app.get('/customers/edit/:id', customers.edit);
24+
app.post('/customers/edit/:id', customers.update);
25+
26+
app.listen(4000, function () {
27+
console.log('Server is running.. on Port 4000');
28+
});

0 commit comments

Comments
 (0)