Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

node.js - Running a web server with pm2 and connecting a psql database

I have this web server: http://35.194.72.130/. My program records times on a stopwatch and gives the user the option to upload their time to a database. This works on my localhost, but I'm having a hard time understanding how to connect my database to the website. enter image description here

My server.js file:

const express = require("express");
const cors = require("cors");

const bodyParser = require("body-parser");
var dateFormat = require('dateformat');

const app = express();
app.set("port", 3001);

app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

const Pool = require("pg").Pool;
const config = {
    host: "35.194.72.130",
    user: "Austin",
    password: "-----",
    database: "cubing"
};

const pool = new Pool(config);

app.post("/addtime", cors(), async (req,res) => {
    const name = req.body.name;
    const time = req.body.time;
    // const timeStamp = dateFormat(time, dateFormat.masks.isoDateTime);
    
    const template = 'INSERT INTO times (name,time) VALUES ($1,$2)';
    const response = await pool.query(template, [name,time]);
    
    res.json({name: name, time: time});

});

app.get("/list", async (req,res) => {
    const template = await pool.query('SELECT * FROM times ORDER BY time ASC');
    res.json({times: template.rows});

})

app.get("/", (req,res) => {
    res.json({ message: "We did it!" });
});

/* app.listen(app.get("port"), () => {
    console.log('Server at: http://localhost:${app.get("port")}/');
}); */

I have one function where I want to retrieve info:

    async componentDidMount() {
        const url = "/list";
        const response = await fetch(url);
        const data = await response.json();
        try {
            this.setState({ person: data.times[0].name, time: data.times[0].time, loading: false });
        } catch (e) {}
        .....
    }

And another where I want to post:

export function addTime(name,time) {
    return fetch('/addtime', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name, time }) 
    })
}

When I look at the page and inspect it, trying to retrieve the database gives me a 404 not found error. Any needed code you need to see I would be happy to deliver. I'm really not sure how to connect my program to the database.

question from:https://stackoverflow.com/questions/65876466/running-a-web-server-with-pm2-and-connecting-a-psql-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...