Hey guys,
I’m having difficulty making GET requests and I’m not sure if it’s my code or maybe I didn’t setup my remote development correctly. I’m using Digital Ocean Node.js droplet “This droplet is all set up with NodeJS, PM2 for process management, and nginx.”
I keep getting a 404 in POSTMAN when I do a GET request to my ip address and when I go to the browser to my ip address/api/posts it says the page can’t be found.
server.js
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose');
const path = require('path');
const app = express()
const postsApi = require('./api/postsApi');
mongoose.connect
(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
app.listen(process.env.PORT, () => {
console.log('Listening to port 5000!')
})
})
//POST form handling
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//Routing
//app.use('*', express.static(path.join(__dirname, '../public/index.html')));
app.use(express.static(path.join(__dirname, 'public')));
//app.use(express.static(path.resolve(__dirname, '../client/build')));
app.use('/api/posts', postsApi);
postsAPI
const fs = require('fs');
const path = require('path');
let express = require('express');
let router = express.Router();
const postsController = require('../controllers/postsController');
const PostService = postsController.PostService;
// routing code
// list
router.get('/allPosts', (req, res, next) => {
PostService.list()
.then((posts) => {
console.log(`API: Found posts: ${posts}`);
res.status(200);
res.json(posts);
}).catch((err) => {
res.status(404);
res.end();
});
});
// export our router
module.exports = router;
postsController
const Post = require('../models/postSchema');
class PostService {
// list
static list() {
return Post.find({})
.then((posts) => {
return posts;
});
}
// findOne
static find(id) {
return Post.findById(id)
.then((post) => {
return post;
});
}
// create
static create(obj) {
var post = new Post(obj);
return post.save();
}
// update
static update(id, data) {
return Post.findById(id)
.then((post) => {
post.set(data);
post.save();
return post;
});
}
// delete
static delete(id) {
return Post.deleteOne({ _id: id })
.then((obj) => {
return obj;
})
}
}
module.exports.PostService = PostService;
postsSchema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let postSchema = new Schema({
title: { type: String, minLength: 5, required: true },
post: { type: String, minLength: 5, required: true }
});
module.exports = mongoose.model("Post", postSchema);
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Hi there,
Do you see any errors in your Node logs? You could check that with
pm2 logs
.Also, I’ve noticed that your route is defined as
/allPosts
, rather than just/posts
, do the GET requests it work with/allPosts
?Also, I could suggest trying to make a GET request directly to the Node service itself rather than the Nginx proxy, that way you can verify that it is not an Nginx problem. If the direct GET requests work fine, can you share your Nginx config here?
Best,
Bobby