如何在数字海洋上托管 nodejS+postgres 应用程序?

How to host nodejS+postgres application on digital-ocean?

我已经按照 traversy media 教程在 digital-ocean 上部署了 nodejs 应用程序。我正在使用数字海洋的 postgres 数据库。但是在 运行 node app.js 命令上我收到错误

我在 Whosebug 上尝试了很多答案,但他们都没有解决问题

app.js

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const db = require('./queries') // contains all query functions
const port = 3000

const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json()) 
app.use(
    bodyParser.urlencoded({
        extended: true,
    })
)
app.use(cors())

app.get('/runs', db.getPlayers)

app.post('/year', db.getByYear)

//Handle production


    app.use(express.static(__dirname+'/public/'))

    //Handle SPA
    app.get(/.*/, (request, response) => response.sendFile(__dirname+'/public/index.html') );


app.listen(port, () => {
    console.log(`App running on port ${port}.`)
})

我得到的错误是:-

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::3000
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1367:14)
    at listenInCluster (net.js:1408:12)
    at Server.listen (net.js:1492:7)
    at Function.listen (/home/vineet/IPL-Stats-Analysis-Dashboard/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/vineet/IPL-Stats-Analysis-Dashboard/app.js:63:5)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)

我不熟悉上述教程 - 但错误表示另一个进程(可能是同一个 express 应用程序)已经在侦听端口 3000。

在 Linux 上,您可以使用命令 ps aux 列出所有 运行 进程。寻找另一个 node 进程。如果有 none - 您可以通过 运行 lsof -Pnl +M -i4 找到哪些进程正在侦听哪些端口 ipv4 地址和 lsof -Pnl +M -i6 ipv6.

或者直接在 Digitalocean droplet 中做 curl http://localhost:3000

如果您的 OS 是 Linux,只需在您的终端上输入 killall -9 node