1+ const express = require ( "express" ) ;
2+ const path = require ( "path" ) ;
3+ const app = express ( ) ;
4+ const mongoose = require ( 'mongoose' ) ;
5+ const bodyparser = require ( "body-parser" ) ;
6+
7+ mongoose . connect ( 'mongodb://localhost/contactDance' , { useNewUrlParser : true } ) ;
8+ const port = 8000 ;
9+
10+
11+ // Define mongoose schema
12+ var contactSchema = new mongoose . Schema ( {
13+ name : String ,
14+ phone : String ,
15+ email : String ,
16+ address : String ,
17+ desc : String
18+ } ) ;
19+
20+ var Contact = mongoose . model ( 'Contact' , contactSchema ) ;
21+
22+ // EXPRESS SPECIFIC STUFF
23+ app . use ( '/static' , express . static ( 'static' ) ) // For serving static files
24+ app . use ( express . urlencoded ( ) )
25+
26+ // PUG SPECIFIC STUFF
27+ app . set ( 'view engine' , 'pug' ) // Set the template engine as pug
28+ app . set ( 'views' , path . join ( __dirname , 'views' ) ) // Set the views directory
29+
30+ // ENDPOINTS
31+ app . get ( '/' , ( req , res ) => {
32+ const params = { }
33+ res . status ( 200 ) . render ( 'home.pug' , params ) ;
34+ } )
35+
36+ app . get ( '/contact' , ( req , res ) => {
37+ const params = { }
38+ res . status ( 200 ) . render ( 'contact.pug' , params ) ;
39+ } )
40+
41+
42+ app . post ( '/contact' , ( req , res ) => {
43+ var myData = new Contact ( req . body ) ;
44+ myData . save ( ) . then ( ( ) => {
45+ res . send ( "This item has been saved to the database" )
46+ } ) . catch ( ( ) => {
47+ res . status ( 400 ) . send ( "Item was not saved to the database" )
48+ } ) ;
49+
50+ // res.status(200).render('contact.pug');
51+ } )
52+
53+ // START THE SERVER
54+ app . listen ( port , ( ) => {
55+ console . log ( `The application started successfully on port ${ port } ` ) ;
56+ } ) ;
0 commit comments