I'm learning Node.JS and as practise I need to create to endpoints:
GET /albums - Get a list of all albums in the dabatase
POST /purchases - Create a purchase
My attempt is as follows:
const mongoose = require('mongoose');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// Imports
const Album = require("./models/album");
const Purchase = require("./models/purchase");
// TODO code the API
// Connect to DB
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
var conn = mongoose.connection;
conn.on('connected', function() {
console.log('database is connected successfully');
});
conn.on('disconnected',function(){
console.log('database is disconnected successfully');
})
conn.on('error', console.error.bind(console, 'connection error:'));
// Routes
app.get('/albums', function(req, res, next) {
Album.find({}, (err, albums) => {
if (!err) {
res.set({
'Content-Type': 'application/json',
'Status': 200,
})
return res.end(JSON.stringify(albums));
} else {
console.log('Failed to retrieve the Course List: ' + err);
}
});
});
// POST method route
app.post('/purchases', (req, res) => {
const purchase = new Purchase({
user: req.body.user,
album: req.body.album
})
purchase.save(function (err, post) {
if (err) { return err }
res.json(201, purchase);
})
})
module.exports = app;
Instructions for GET Request:
Since this is a JSON API, return JSON and a 200 status code, with the exception of destroy method which should return a 204 status code indicating no content.
All three Album columns title, performer and cost should be returned in a data object for the GET, POST and PUT methods. Here is an example of the format of response.body.data:
Expected Form:
response.body.data = {
_id: "the id of the album",
title: "Appetite for Destruction",
performer: "Guns N' Roses",
cost: 20
};
Instructions for POST Request:
The POST /purchases route should expect user and album properties to be set in the request body. It should then store a reference to both of these records on the newly created purchase record.
The response for POST /purchases should include the purchase record as well as the user and album relations, which should be populated with all their data fields.
Album Schema:
const albumSchema = mongoose.Schema({
performer: String,
title: String,
cost: Number
});
Purchase Schema:
const purchaseSchema = mongoose.Schema({
user: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
album: {type: mongoose.Schema.Types.ObjectId, ref: "Album"}
})
The program need to pass the follwing two test cases for these endpoints:
describe("GET /albums", () => {
it("should return an array of all models", async () => {
const album = new Album(albumData).save();
const res = await chai
.request(app)
.get("/albums")
;
expect(res.status).to.equal(200);
expect(res).to.be.json;
expect(res.body.data).to.be.a("array");
expect(res.body.data.length).to.equal(1);
expect(res.body.data[0].title).to.equal(albumData.title);
expect(res.body.data[0].performer).to.equal(albumData.performer);
expect(res.body.data[0].cost).to.equal(albumData.cost);
}).timeout(2000);
});
describe("POST /purchases", () => {
it("should create a new purchase and return its relations", async () => {
const otherAlbumData = {
title: "Sample",
performer: "Unknown",
cost: 2,
};
const album = await new Album(otherAlbumData).save();
const user = await new User({name: "James"}).save();
const res = await chai
.request(app)
.post("/purchases")
.send({user, album})
;
expect(res.status).to.equal(200);
expect(res).to.be.json;
expect(res.body.data).to.haveOwnProperty("user");
expect(res.body.data.user).to.haveOwnProperty("name");
expect(res.body.data).to.haveOwnProperty("album");
expect(res.body.data.album).to.haveOwnProperty("title");
expect(res.body.data.user.name).to.equal(user.name);
expect(res.body.data.album.title).to.equal(album.title);
}).timeout(2000);
});
});
The problem is that GET /albums doesn't properly fetch the data. Error: "expected undefined to be an array" while POST /purchases throws error 500, "Cannot read property 'user' of undefined" but as per description "route should expect user and album properties to be set in the request body".
Can somebody gives me a headsup? I'm fairly new to Node.JS. Thanks.
you should add following code before Routes:
app.use(express.json({ limit: '15kb' }))
app.use(express.urlencoded({ extended: false }))
Related
I'm currently learning angular and working on a project with a mongoDB database and express for my APIs. I want to fetch the comments of a post by the post ID,
The get request returns me a list of comments. the problem is when I first run node js the get request doesn't work, it only works when I first post a new comment and then run the get request for the comments again.
And as long as node is running the get request will continue to work whenever it's called for, until I restart node once again for the error to happen again.
it returns a 404 not found error.
This error doesn't happen with any other route, but my code is the same in all of them.
PS : I Have made sure that the function is getting the post id before the get request is made.
this is my server.js file
let express = require('express'),
path = require('path'),
mongoose = require('mongoose'),
cors = require('cors'),
bodyParser = require('body-parser'),
dbConfig = require('./database/db');
//create Error definition
const createError = require('http-errors');
// Connecting with mongo db
mongoose.Promise = global.Promise;
mongoose.connect(dbConfig.db, {
useNewUrlParser: true
}).then(() => {
console.log('Database sucessfully connected')
},
error => {
console.log('Database could not connected: ' + error)
}
)
const userRoute = require('./routes/user.route');
const postRoute = require('./routes/post.route');
const galleryRoute = require('./routes/Gallery.route');
const likeRoute = require('./routes/Like.Route');
const commentRoute = require('./routes/Comment.route');
const shareRoute = require('./routes/Share.route');
const profilePicRoute = require('./routes/ProfilePic.route');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors());
app.use(express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/', express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/api/users', userRoute);
app.use('/api/posts', postRoute);
app.use('/api/likes', likeRoute);
app.use('/api/profilePics', profilePicRoute);
app.use('/api/comments', commentRoute);
app.use('/api/shares', shareRoute);
app.use('/api/gallery', galleryRoute);
// Create port
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
console.log('Connected to port ' + port)
})
// Find 404 and hand over to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
console.error(err.message); // Log error message in our server's console
if (!err.statusCode) err.statusCode = 500; // If err has no specified error code, set error code to 'Internal Server Error (500)'
res.status(err.statusCode).send(err.message); // All HTTP requests must have a response, so let's send back an error with its status code and message
});
this is my commentRoute.js
const express = require('express');
const commentRoute = express.Router();
// Comment model
let Comment = require('../models/Comment');
const createError = require('http-errors');
//multer for pic upload
const uploadMedia = require('../middleware/picUpload')
// Add Comment
commentRoute.route('/create').post((req, res, next) => {
// if(req?.files[0]){
// newComment.media = req?.files[0]
// }
let newComment = req.body;
newComment.creationDate = new Date(req.body.creationDate)
console.log(newComment)
Comment.create(newComment, (error, data) => {
// if (error instanceof multer.MulterError ) {
// error.message += "\nmulter Error";
// return next(error)
// }else
if (error){
return next(error)
}
else {
res.json(data);
}
})
//Get comments by parent ID
commentRoute.route('/read/byParentId/:idParent').get( async (req, res, next) => {
await Comment.find({idParent : req.params.idParent}, (error, data) => {
if(error){
return next(error)
}else{
res.json(data)
}
})
})
})
module.exports = commentRoute;
this is my mongoose comment schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Define collection and schema
let Comment = new Schema({
idUser: {
type : String
},
idParent : {
type : String
},
text : {
type : String
},
media : {
fieldname : { type : String },
originalname : { type : String },
encoding : { type : String },
mimetype : { type : String },
buffer : { type : Buffer },
},
creationDate : {
type : Date
}
},
{
collection: 'comments'
})
module.exports = mongoose.model('Comment', Comment);
this is my client side http get request
baseUrl = 'http://localhost:4000/api/comments';
headers = new HttpHeaders().set('Content-Type', 'application/json');
constructor(private http : HttpClient) { }
getCommentsByParentId(idParent : any){
return this.http.get(`${this.baseUrl}/read/byParentId/${idParent}`);
}
this is how I consume the api in the client side
getComments(){
this.commentService.getCommentsByParentId(this.idPost).subscribe({
next : (res : any) => {
this.comments = res
this.commentsCount = res.length
},
error : (err : any) => {
console.log("error getting comment list for post "+this.idPost)
}
})
}
client side error :
server side error :
thank you.
Edit :
post without the list of comments before I post a new comment
post after I post a new comment
Well, that's very obvious that the server can't find the entity in the DB.
You need to check one of the following:
Maybe when you restart the node server, you restart the db too. that can happen if you're using docker-compose locally. then when you run your node server again your DB starts but there's no data in the DB, therefore the service can't find any data.
After service restart you're using non-existing ID because of wrong UI flow.
I would guess that you're facing the first option.
so I already have the data in Mongo, but the problem is the Mongo doesn't read properly the data from the backend. This is my backend code:
app.get ('/posts', async (req , res) => {
let db = await connect();
let posts = req.body;
res.send(posts);
});
and the code from services:
async get_posts() {
let response = await Service.get(/posts)
let doc = response.doc;
return {
id: doc._id,
email: doc.email,
title: doc.title,
imageDesc: doc.imageDesc,
img: doc.img,
};
}
You need to use the instance of the db, and an instance to the collection in order to retrieve the posts.
For instance:
app.get ('/posts', async (req , res) => {
const postsCollection = db.collection("posts");
const posts = await postsCollection.find();
return res.json(posts);
});
Read the mongo documentation here
I have a list of articles that have a property views and I want to increment that property in the database each time a user clicks on an article title. Currently nothing happens when I do it. Why isn't it working and how can I increment that property each time on click? Here is my React part:
const incrementViews = (id) => {
var item = posts.find(x => x._id === id);
item.views += 1;
}
<div className="post-title">
<Link to={`/post/${post._id}`}>
<h2><a href="#" onClick={() => incrementViews(post._id)}>{post.title}</a>
</h2>
</Link>
</div>
and my server.js:
// Requiring the dependencies
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
require('dotenv').config();
const mongoose = require('mongoose');
const PORT = process.env.PORT || 3001;
const BASE_URL = process.env.REACT_APP_BASE_URL;
console.log(BASE_URL)
const itemRoutes = express.Router();
let Comment = require('./comment.model');
app.use(cors());
app.use(bodyParser.json());
mongoose.connect(BASE_URL, { useNewUrlParser: true })
const connection = mongoose.connection;
connection.once('open', function () {
console.log('Connection to MongoDB established succesfully!');
});
let collection = connection.collection("posts_with_tags_test");
collection.createIndex(
{
postContent: 'text',
title: 'text'
}
);
// Serve static assets
if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}
itemRoutes.route('/').get(async (req, res) => {
let collection = connection.collection("posts_with_tags_test");
let response = await collection.find({})
.toArray();
res.send(response);
});
itemRoutes.route('/search').post(async (req, res) => {
let result = await connection.collection("posts_with_tags_test").find({
$text: {
$search: req.body.searchString
}
}).toArray();
res.send(result);
});
itemRoutes.route("increment/:id"").post(async (req, res) => {
const { id } = req.params;
collection.updateOne({ _id: id }, { $inc: { views: 1 } });
return res.status(200).json({ msg: "OK" });
});
itemRoutes.route('/comments').get(async (req, res) => {
let collection = connection.collection("comments");
let response = await collection.find({})
.toArray();
res.send(response);
});
itemRoutes.route('/comments')
.post((req, res) => {
res.setHeader('Content-Type', 'application/json');
let comment = new Comment(req.body);
comment.save()
.then(comment => {
res.status(200).json({ comment })
})
.catch(err => {
res.status(400).send('failed')
})
});
app.use('/', itemRoutes);
app.use('/comments', itemRoutes);
app.use('/search', itemRoutes);
app.use('/increment', itemRoutes);
app.listen(PORT, function () {
console.log('Server is running on' + ' ' + PORT);
})
I think there are two problems in frontend and backend respectively.
Front-end
You should use post variable as a state variable so as to re-render then component when changes are made on post.
Back-end
There is no issue with increasing view in your code.
Here, you need to return success status.
The function incrementViews only increments views on the frontend and never sends any data to the API. One way you can make it work is as follows:
server.js
itemRoutes.route("/increment/:id").post(async (req, res) => {
const { id } = req.params;
collection.updateOne({ _id: id }, { $inc: { views: 1 } });
return res.status(200).json({ msg: "OK" });
});
React
const incrementViews = (id) => {
// Assuming your API server is running on port 5000.
fetch(`http://localhost:5000/increment/${id}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((res) => res.json())
.then(console.log)
.catch(console.error);
};
Update
The reason you're getting 404 is a missing colon : in the route parameters.
// Notice the :id, colon is important.
itemRoutes.route("/increment/:id").post(async (req, res) => {
const { id } = req.params;
// ...
});
Here is a demo reproduced on Glitch. Removed database logic and just added a response messages.
I tested the demo using Postman and it works fine.
On a POST request to https://adaptive-sassy-legal.glitch.me/increment/123, should return a response as shown below.
{
msg: "itemsRoute increment.",
id: "123"
}
Update 2
Another thing which I forgot to mention in the previous update was to update the middleware.
// Use only `/increment` instead of `increment/:id`.
app.use("/increment", itemRoutes);
Here is an updated demo.
I have an app that uses node and stripe to make payments, however, when I hid the key in the .env file and plugged it in, it will not work. I can log out the key by calling process.env.STRIPE_SECRET_KEY.
const express = require("express");
const router = express.Router();
const env = require("dotenv").config({ path: "../../.env" });
// const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
console.log(process.env.STRIPE_SECRET_KEY);
router.post("/", function (req, res) {
console.log(req.body.charge.amount);
const token_id = req.body.charge.source;
const purchase_price = req.body.charge.amount;
const email = req.body.receipt_email;
console.log(req.body.receipt_email);
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
// `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
stripe.charges.create(
{
amount: 1000,
currency: "usd",
source: "tok_visa",
description: "My First Test Charge (created for API docs)",
receipt_email: email,
},
function (err, charge) {
if (err && err.type === "StripeCardError") {
res.json({ status: "failure", reason: "card was declined" });
} else {
console.log(charge);
res.json({ status: "Success!" });
}
}
);
});
I am wondering if anyone can help me out here. I'm currently taking a Udemy course on learning how to connect a database with my server. I have been going word by word on the teacher's code, frequently checking if my code has any minor errors. I didn't have any trouble with code thus far on my server.js file. Port is running smoothly. However, when I run the POST request from Postman, I get
Unhandled rejection Error: Connection terminated unexpectedly
Through Postman, I am following exactly what the teacher did. The POST request contains email, password, name and the localhost:3000/register path is fine. That is what my server is connected to. I'm wondering what's going on since my code runs smoothly until I do a POST request. Also noting that I get 200 OK response on Postman but on server, I get that unhandled rejection error message.
Yeah that console log is intentional, I am going along exactly what he is doing in the video so code is bound to change over time on the next video.
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require ('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const pg = require('pg');
const db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
port: '3000',
password : '',
database : 'smart-brain'
}
});
const app = express();
const database = {
users: [
{
id: '123',
name: 'Jess',
email: 'jess#gmail.com',
password: 'cookies',
entries: 0,
joined: new Date()
},
{
id: '124',
name: 'Sally',
email: 'sally#gmail.com',
password: 'bananas',
entries: 0,
joined: new Date()
}
]
}
app.use(bodyParser.json());
//body parser is basically json.parse. we want to always parse json so our code is readable in string form. POST > Raw > JSON
app.use(cors())
app.get('/', (req, res)=> {
res.send(database.users);
})
app.post('/signin', (req, res) => {
if(req.body.email === database.users[0].email && req.body.password === database.users[0].password) {
res.json(database.users[0])
} else {
res.status(400).json('error logging in')
}
})
app.post('/register', (req, res) => {
const { email, name, password } = req.body;
db('users').insert({
email: email,
name: name,
joined: new Date()
}).then(() => console.log())
res.json(database.users[database.users.length-1])
});
app.get('/profile/:id', (req, res) => {
const { id } = req.params;
let found = false;
database.users.forEach(user => {
if (user.id === id) {
found = true;
return res.json(user);
}
})
if (!found) {
res.status(400).json('not found...')
}
})
//now we are creating route for entries count. everytime they submit image, they will get a count for it
app.put('/image', (req, res) => {
const { id } = req.body;
let found = false;
database.users.forEach(user => {
if (user.id === id) {
found = true;
user.entries++
return res.json(user.entries);
}
})
if (!found) {
res.status(400).json('not found...')
}
})
app.listen(3000, ()=> {
console.log('app is running on port 3000');
})
try to add catch function after then block.
app.post('/register', (req, res) => {
const { email, name, password } = req.body;
db('users').insert({
email: email,
name: name,
joined: new Date()
}).then(() => {
res.json(database.users[database.users.length-1])
}).catch((err)=>{console.log(err)})
});