6

I'm trying to host my server.js file that has my API configured on Cyclic, the deployment is a success but the URL is returning status 500 for any endpoint call. I hosted the React front-end on github pages. I have no idea what's missing or why it's not responding to any calls since the deployment was a success. Is it because I don't have an endpoint configured to '/' ?

server.js code:


const express = require('express')
const cors = require('cors')
require("dotenv").config()
const {getDb, dbConnect} = require('./db')
const app = express()


app.use(cors({origin: 'https://mygithubpage.github.io/'}))
app.use(express.json())

let db
const port = process.env.PORT || 6900;
dbConnect((error) => {
    if(!error) {
        app.listen(port, () => {
            console.log(`Now listening to port ${port}`)
        })
        db = getDb()
    }   
})

// Searching for email and password
app.post('/signIn', (req,res) => {
    
    const user = req.body

    db.collection('users')
    .findOne({email: user.email, password: user.password})
    .then(user => {
        res.status(200).json(user)
    })
    .catch(error => {
        res.status(500).json({error: 'user not found'})
    })
})

Cyclic deployment log:

2023-04-02T20:00:47.283Z: [CYCLIC] cloning...
2023-04-02T20:00:48.776Z: From https://github.com/mygithubpage/movie-app
 * branch            ddd079a5bf40fbb8e440632fcdd91b584265c9cb -> FETCH_HEAD
2023-04-02T20:00:49.984Z: HEAD is now at ddd079a added node in package.json
2023-04-02T20:00:49.998Z: [CYCLIC] Building...
2023-04-02T20:00:50.023Z: Build Configuration:
  Root Path: /server
  Output Path: /
  Static Site: false
  Runtime: nodejs18.x
  Branch: main
  Ref: ddd079a5bf40fbb8e440632fcdd91b584265c9cb

2023-04-02T20:00:50.238Z: [CYCLIC] verifying...
2023-04-02T20:00:50.308Z: [CYCLIC] using: node:v18.15.0 npm:10.1.0 runtime:nodejs18.x
[CYCLIC] building from: /server
2023-04-02T20:00:50.317Z: [CYCLIC] installing dependencies from: package-lock.json
2023-04-02T20:00:56.440Z: 
added 110 packages in 6s
2023-04-02T20:00:56.456Z: [CYCLIC] running build if defined...
2023-04-02T20:00:56.950Z: [CYCLIC] pruning dev dependencies...
2023-04-02T20:00:57.746Z: 
removed 2 packages in 353ms
2023-04-02T20:00:57.773Z: [CYCLIC] packaging 109.36 MB...
2023-04-02T20:00:57.775Z: [CYCLIC] bundling from  ...
2023-04-02T20:01:02.914Z: [CYCLIC] done packaging
[CYCLIC] deploying...
2023-04-02T20:01:11.058Z: deployed ca-central-1 -  8.062s
2023-04-02T20:01:11.059Z: SUCCESS

took 24.1 seconds
api deployed at:
https://gleaming-plum-horse.cyclic.app

I tried using other websites like www.render.com but the deployment fails everytime after a timeout and a multiple logs of starting service 'npm start'/'node server.js'

I'm getting this error whenever I'm sending a GET/POST request to the server

2023-04-03 13:11:35.635: grep: /var/task/package.json: No such file or directory
2023-04-03 13:11:35.665: grep: /var/task/package.json: No such file or directory
ERROR: Cannot find entry point.
2023-04-03 13:11:42.344: 
          ERROR: Application process finished with status code 0 before starting a server

          Common causes/solutions for this include:

            - Server listen method not called. Verify server is listening on a port: "app.listen(process.env.PORT||3000)"
            - An error may have been caught without being logged. Verify try/catch blocks have appropriate logging.

The server.js environment was working just fine locally.

1 Answers1

0

I think you are missing the app.listen() method.

https://expressjs.com/en/starter/hello-world.html

The link above should give you a good idea of what you are missing, unless your snippet you shared above has been truncated.

const express = require('express');
const app = express();

app.get("/", (req, res) => {
    res.status(200).json( { msg: "Helloaaa" });
});

app.listen(7000, () => {
    console.log(`The app has started on PORT 7000`)
});

The above snippet is also an example that might direct you towards what might be wrong. We initialize the GET API and then open up the application to listen in on port 7000.

Without the port, the application is not listening to any port hence there will be no response.

ark_knight
  • 21
  • 6