-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (65 loc) · 2.45 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
// sets up express application and can configure and add functionality to server
const app = express();
const path = require('path');
// loads dependencies
var bodyParser = require('body-parser');
// loads package
const nodemailer = require(`nodemailer`);
// environment variabels from .env file
require('dotenv').config();
// var smtpTransport = nodemailer.createTransport("SMTP", {
// service: 'Gmail',
// auth: {
// // enter your gmail account
// user: 'GMAIL_USER',
// // enter your gmail password
// pass: 'GMAIL_PASS'
// }
// });
// logs to server all requests, see on terminal
const urlLogger = (request, response, next) => {
console.log('Request URL:', request.url, request.method);
next();
};
app.use(urlLogger);
// specified path for all static asset files
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (request, response) => {
// send file at ./index location, server sends index.html to user
response.sendFile(path.join(__dirname, "./index.html"));
});
// app.get('/index.html', (request, response) => {
// // send file at ./index location, server sends index.html to user
// response.sendFile(path.join(__dirname, "./index.html"));
// });
app.get('/json', (request, response) => {
// send 200 status to notify client that response was successful
response.status(200).json({"name": "Robbie"});
});
// tells server to listen for connections on a particular port (3000)
app.listen(3000, () => {
// when ready to listen for connections, callback is called, logging the below message
console.log('Express intro running on localhost:3000');
});
// RECEIVING POST REQUEST FROM javascript.JS
// when post to server of /send, this function runs
// app.post('/send', function (req, res) {
// var mailOptions = {
// to: req.query.to,
// subject: 'Contact Form Message',
// from: "Contact Form Request" + "<" + req.query.from + '>',
// html: "From: " + req.query.name + "<br>" +
// "User's email: " + req.query.user + "<br>" + "Message: " + req.query.text
// }
// console.log(mailOptions);
// smtpTransport.sendMail(mailOptions, function (err, response) {
// if (err) {
// console.log(err);
// res.end("error");
// } else {
// console.log("Message sent: " + response.message);
// res.end("sent");
// }
// });
// });