Procházet zdrojové kódy

Now you can specify which host the server will bind

Brendan Abolivier před 8 roky
rodič
revize
f467f47481
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 změnil soubory, kde provedl 17 přidání a 3 odebrání
  1. 13
    1
      README.md
  2. 4
    2
      server.js

+ 13
- 1
README.md Zobrazit soubor

@@ -2,7 +2,7 @@
2 2
 
3 3
 Always wanted to implement a contact form in your website and/or portfolio, but don't want to busy yourself with something too complex (mail sending in PHP, for example, is a complete mess)? Here's a miracle solution for ya! Just run the nodemailer-based app, include a JavaScript file in your HTML page, and you're all set :wink:
4 4
 
5
-## Install
5
+## Install and run
6 6
 
7 7
 Just clone this repository, edit the `settings.json` file (described below) and run the server:
8 8
 
@@ -19,6 +19,18 @@ The default port will be set to `1970`, but you can set the one you want by usin
19 19
 PORT=8080 npm start
20 20
 ```
21 21
 
22
+Same goes with the host. Without further instructions, the server will listen on 0.0.0.0, which means it will accept every connection, whatever the source. You can override this by using the `HOST` environment variable:
23
+
24
+```bash
25
+HOST=127.0.0.1 npm start
26
+```
27
+
28
+So, if we want our server to only listen to requests from its host, on the 8080 port, we'll start the server like this:
29
+
30
+```bash
31
+HOST=127.0.0.1 PORT=8080 npm start
32
+```
33
+
22 34
 Obviously, you'll need Node.js and NPM (or any Node.js package manager) to run the app. As we're launching a webserver (which will serve the necessary files and process the mail sending requests), this app will run continuously. One good practice would be to run it as a daemon (in a systemd service, for example).
23 35
 
24 36
 ## Usage

+ 4
- 2
server.js Zobrazit soubor

@@ -105,9 +105,11 @@ app.post('/send', function(req, res, next) {
105 105
 
106 106
 // Use either the default port or the one chosen by the user (PORT env variable)
107 107
 var port = process.env.PORT || 1970;
108
+// Same for the host (using the HOST env variable)
109
+var host = process.env.HOST || '0.0.0.0';
108 110
 // Start the server
109
-app.listen(port, function() {
110
-    log.info('Server started on port ' + port);
111
+app.listen(port, host, function() {
112
+    log.info('Server started on ' + host + ':' + port);
111 113
 });
112 114
 
113 115