express-template/www

111 lines
2.2 KiB
Plaintext
Raw Permalink Normal View History

2021-08-16 14:29:43 +00:00
#!/usr/bin/env node
/**
* Module dependencies.
*/
const { resolve } = require('path')
const package = require('./package.json')
2021-08-29 10:31:21 +00:00
require('dotenv').config({ path:resolve('./.env') })
if(process.env.NODE_ENV === 'production') {
process.chdir(resolve(__dirname,'dist'))
}
var app = require(resolve('./app'))
2021-08-16 14:29:43 +00:00
var debug = require('debug')(package.name + ':server')
var http = require('http')
2021-08-29 10:31:21 +00:00
const { event } = require(resolve('./event'))
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Get port from environment and store in Express.
*/
2021-08-16 14:29:43 +00:00
var port = normalizePort(process.env.PORT || '3000')
app.set('port', port)
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Create HTTP server.
*/
2021-08-16 14:29:43 +00:00
var server = http.createServer(app)
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Listen on provided port, on all network interfaces.
*/
// event.on('system-config-sync-complete',()=>{
// server.listen(port,()=>{
// console.log(`服务启动成功,监听端口${port}`)
// event.emit('http-server-started')
// })
// })
event.on('typeorm-connected',()=>{
2021-08-16 14:29:43 +00:00
server.listen(port,()=>{
console.log(`服务启动成功,监听端口${port}`)
2021-08-29 10:31:21 +00:00
event.emit('http-server-started')
2021-08-16 14:29:43 +00:00
})
})
server.on('error', onError)
server.on('listening', onListening)
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Normalize a port into a number, string, or false.
*/
2021-08-16 14:29:43 +00:00
function normalizePort (val) {
var port = parseInt(val, 10)
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
if (isNaN(port)) {
// named pipe
return val
}
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
if (port >= 0) {
// port number
return port
}
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
return false
}
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Event listener for HTTP server "error" event.
*/
2021-08-16 14:29:43 +00:00
function onError (error) {
if (error.syscall !== 'listen') {
throw error
}
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges')
process.exit(1)
break
case 'EADDRINUSE':
console.error(bind + ' is already in use')
process.exit(1)
break
default:
throw error
}
}
2021-08-29 10:31:21 +00:00
2021-08-16 14:29:43 +00:00
/**
2021-08-29 10:31:21 +00:00
* Event listener for HTTP server "listening" event.
*/
2021-08-16 14:29:43 +00:00
function onListening () {
var addr = server.address()
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port
debug('Listening on ' + bind)
}