使用NodeJS / ExpressJS的自定义本地主机名称

时间:2019-02-21 17:34:23

标签: node.js express https dns

我使用NodeJS和ExpressJS创建Web应用程序,我使用https服务器。我希望连接到同一网络的所有设备不要输入本地主机的IP地址,而是可以输入例如https://SmartphoneMonitoring.com并将其重定向到本地主机

服务器脚本:

    var connections = [];
var express = require('express');
var bodyParser = require('body-parser')
var expressValidator = require('express-validator');
var hbs = require('hbs'); //handlebars
var session = require('express-session');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var routes = require('./routes/router');
var https = require('https');
var path = require('path');
var mysql = require('mysql');
var dns = require('dns');

dns.resolve4('www.google.com', function (err, addresses) {
    if (err) throw err;

    console.log('addresses: ' + JSON.stringify(addresses));

    addresses.forEach(function (a) {
        dns.reverse(a, function (err, domains) {
            if (err) {
            console.log('reverse for ' + a + ' failed: ' + err.message);
            } else {
            console.log('reverse for ' + a + ': ' + JSON.stringify(domains));
            }
        });
    });
});

var databasePort = 3306;
var dataBaseName = 'BazaDanych';
const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    port: databasePort,
    database: dataBaseName
});

setTimeout(function(){connectToDB()},1000);

function connectToDB(){
    db.connect((err) =>{
        if(err){
            console.log(err);
        } else {
            console.log('Połączono z bazą danych na porcie: ' + databasePort)       
        }

    });
};

var app = express();

const httpsOptions = {
    cert: fs.readFileSync(path.join(__dirname, 'ssl', 'server.crt')),
    key: fs.readFileSync(path.join(__dirname, 'ssl', 'server.key'))
};
var server = https.createServer(httpsOptions, app);
var io = require('socket.io', { rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling'] }).listen(server);

app.set('views', (__dirname, 'views'));
app.set('view engine', '.hbs');

hbs.registerHelper('json', function(obj){
    return new hbs.SafeString(JSON.stringify(obj));
})

//Partials 
const partialsDir = __dirname + '/views/partials';

const filenames = fs.readdirSync(partialsDir);

filenames.forEach(function (filename) {
  const matches = /^([^.]+).hbs$/.exec(filename);
  if (!matches) {
    return;
  }
  const name = matches[1];
  const template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
  hbs.registerPartial(name, template);

});
//Partials

hbs.registerHelper('ifequal',function(a, b,options)
{
    if (a==b){
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});


var serverPort = 3030;
server.listen(serverPort, function(){
        console.log("Połączono z serwerem na porcie: " + serverPort);
    })
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator());
app.use(cookieParser());
app.use(session({
    key: 'user_sid',
    secret: 'fsdfdsfsdfsdfsdfsdf',
    resave: false,
    saveUninitialized: false
}));

app.use('/', routes);

我尝试使用DNS来获得效果,但是并没有带来预期的结果。

0 个答案:

没有答案