I am designing a middleware between the client and the GraphQL API. The idea is to create a route that all client requests go to first /filament. This route then decides whether it can send data back from the cache or to proceed on to /graphql to access the resolvers there to get data from the database. I am using Axios to make my requests but it isn't firing server side. I wanted to ask if this was just an improper way to use Axios or if I am doing it incorrectly.
Server.js
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const fetch = require("node-fetch");
const redis = require("redis");
const PORT = process.env.PORT || 4000;
// const REDIS_PORT = process.env.REDIS_PORT || 6379;
//Create Redis client on Redis port (optional)
const client = redis.createClient();
const schema = require('./schema');
const bluebird = require('bluebird')
const app = express()
// bluebird.promisifyAll(redis.RedisClient.prototype);
// bluebird.promisifyAll(redis.Multi.prototype);
app.use(express.json())
client.on("error", err => {
console.log("Error " + err);
});
client.on('connect', () => {
console.log('Redis client connected');
});
// pass redis as context so our schema can use Redis methods
const wrapper = require('./filamentMiddleware')
app.use('/filament',
wrapper(client), // filamentMiddleware with access to client
)
app.use(
'/graphql',
graphqlHTTP((req) => ({
schema,
graphiql: true,
context: {
client,
req: req
}
})),
// addToCacheWrapper(res.data)
);
app.listen(PORT, () =>
console.log(`GraphQL server is running on port: ${PORT}`)
);
Middleware
const axios = require('axios')
const serverFilamentQuery = require('./serverFilamentQuery')
const mergeDataFromCacheAndServer = require('./mergeDataFromCacheAndServer')
const wrapper = (client) => (req, res, next) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
client.get(ip, (err, cacheData) => {
const { query } = req.body
// ip not found in cache
if (!cacheData) {
console.log('cacheData:', cacheData)
axios.post('/graphql', { query }).then(res => {
console.log('have we made the post ????')
// set redis cache
// set the new data in Redis
client.set(ip, JSON.stringify({
todos: res.data.data['todos']
}), (err, result) => {
console.log(result)
})
const { data } = res.data
// return combinedData to client
return res.status(200).json({ data })
})
}
// console.log('ip found in cache')
const [newQuery, data, isMatched] = serverFilamentQuery(query, cacheData)
if (isMatched) {
return res.status(200).json({ data })
} else {
axios.post('/graphql', { newquery }).then(res => {
const merged = mergeDataFromCacheAndServer(data['todos'], res.data.data['todos']);
// set the new data in Redis
console.log(merged)
client.set(ip, JSON.stringify({
todos: merged
}), (err, result) => {
console.log(result)
})
// return combinedData to client
return res.status(200).json({ merged })
})
}
})
}
module.exports = wrapper
Yes, there is no problem making a post request server-side with Axios.
As per the Axios GitHub page
Promise based HTTP client for the browser and node.js (ie. server-side)
Note that the server-side call/usage is slightly different though, as per the docs here
versus client-side call here
Related
I'm testing the endpoint for /api/sentiment in postman and I'm not sure why I am getting the cannot POST error. I believe I'm passing the correct routes and the server is listening on port 8080. All the other endpoints run with no issue so I'm unsure what is causing the error here.
server.js file
const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
const db = require("./app/models");
const Role = db.role;
db.mongoose
.connect(`mongodb+srv://tami00:MEUxClWqUNbLz359#cluster0.gmvao.mongodb.net/test?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Successfully connect to MongoDB.");
initial();
})
.catch(err => {
console.error("Connection error", err);
process.exit();
});
// simple route
app.use('/api/favourite', require('./app/routes/favourite.routes'));
app.use('/api/review', require('./app/routes/review.routes'));
app.use('/api/sentiment', require('./app/routes/sentiment-analysis.routes'));
// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
function initial() {
Role.estimatedDocumentCount((err, count) => {
if (!err && count === 0) {
new Role({
name: "user"
}).save(err => {
if (err) {
console.log("error", err);
}
console.log("added 'user' to roles collection");
});
new Role({
name: "creator"
}).save(err => {
if (err) {
console.log("error", err);
}
console.log("added 'creator' to roles collection");
});
new Role({
name: "watcher"
}).save(err => {
if (err) {
console.log("error", err);
}
console.log("added 'watcher' to roles collection");
});
}
});
}
sentiment-analysis routes file
const express = require('express');
const router = express.Router();
const getSentiment = require('../sentiment-analysis/sentimentAnalysis')
router.post('/api/sentiment', (req, res) => {
const data = req.body.data
const sentiment = getSentiment(data)
return res.send({sentiment})
})
module.exports = router;
sentimentAnalysis.js file
const aposToLexForm = require("apos-to-lex-form");
const {WordTokenizer, SentimentAnalyzer, PorterStemmer} = require("natural");
const SpellCorrector = require("spelling-corrector");
const stopword = require("stopword");
const tokenizer = new WordTokenizer();
const spellCorrector = new SpellCorrector();
spellCorrector.loadDictionary();
const analyzer = new SentimentAnalyzer('English', PorterStemmer, 'afinn')
function getSentiment(text){
if(!text.trim()) {
return 0;
}
const lexed = aposToLexForm(text).toLowerCase().replace(/[^a-zA-Z\s]+/g, "");
const tokenized = tokenizer.tokenize(lexed)
const correctSpelling = tokenized.map((word) => spellCorrector.correct(word))
const stopWordsRemoved = stopword.removeStopwords(correctSpelling)
console.log(stopWordsRemoved)
const analyzed = analyzer.getSentiment(stopWordsRemoved);
console.log(analyzed)
}
module.exports = getSentiment;
console.log(getSentiment("Wow this is fantaztic!"))
console.log(getSentiment("let's go together?"))
console.log(getSentiment("this is so bad, I hate it, it sucks!"))
I see that you use your routes like: app.use('/api/sentiment', require('./app/routes/sentiment-analysis.routes'));. But then in your sentiment-analysis you again use /api/sentiment so your request URL should be /api/sentiment/api/sentiment
Shouldn't it be:
const data = req.body.data
I am building a restful API to fetch data from an external API (https://api.exchangeratesapi.io/latest). The fetch is successful. However, I want a situation where if you query like so "/api/rates?currency=EUR,GBP,USD", you will get
"rates": {
"EUR": 0.0377244605,
"GBP": 0.033795458,
"USD": 0.044824204
}
And if there is no query parameter, all the rates will be returned.
I tried using ternary operator to pull out the figures when a query parameter is present, but I get the error "TypeError: Cannot read property 'rates' of undefined"
Below is my app.js file
const express = require("express");
const request = require('request');
const apiCallFromRequest = require('./Requests')
const app = express();
const exchangeRouter = express.Router();
const port = process.env.PORT || 4001;
exchangeRouter.route(`/rates`)
.get((req, res) => {
const queryParameter = req.query;
// console.log(queryParameter.currency)
apiCallFromRequest.callApi(function(response) {
let cur = queryParameter.currency
res.json({
results: {
base: queryParameter.base ? queryParameter.base : response.base,
date: response.date,
rates: queryParameter.currency ? queryParameter.currency.response.rates : response.rates
}
})
res.end();
});
});
app.use('/api', exchangeRouter);
app.get('/', (req, res) => {
res.send('Welcome to my nodemon API')
});
app.listen(port, () => {
console.log(`Running on port ${port}`);
})
And my Request.js file
const request = require('request');
const URL = `https://api.exchangeratesapi.io/latest`;
const callExternalApiUsingRequest = (callback) => {
request(URL, { json: true }, (err, res, body) => {
if (err) {
return callback(err);
}
// console.log("body: ", body)
return callback({
base: body.base,
date: body.date,
rates: body.rates
});
});
}
module.exports.callApi = callExternalApiUsingRequest;
As I can see you're trying to filter the response. It would be better to inject your query params into your external api call.
You're expecting currency as a query parameter /api/rates?currency=EUR,GBP,USD on your api. There is no response under currency that's why queryParameter.currency.rates is undefined
According to https://exchangeratesapi.io/ you can use symbols to achieve the same result:
GET https://api.exchangeratesapi.io/latest?symbols=USD,GBP HTTP/1.1
you may use the same query params as the external api for yours and use a function to inject them into the external call
The solution. My app.js file should look like this:
const express = require("express");
const request = require('request');
const apiCallFromRequest = require('./Requests')
const app = express();
const exchangeRouter = express.Router();
const port = process.env.PORT || 4001;
exchangeRouter.route(`/rates`)
.get((req, res) => {
apiCallFromRequest.callApi(function(response) {
if (!base && !currency) {
return res.status(200).json({
results: {
"base": response.base,
"date": response.date,
"rates": response.rates
}
})
}
// If there is a query parameter but no value
if (!base) {
return res.status(400).json({ message: "Please specify your home currency" })
}
if (!currency) {
return res.status(400).json({ message: "Please specify exchange currency or currencies" })
}
// If there is query parameter and a value
const exchangeRates = currency.toUpperCase().split(',')
let currencyExchangeRates = {};
for (const exchangeRate of exchangeRates) {
//trim white space
const value = exchangeRate.trim();
currencyExchangeRates = { ...currencyExchangeRates, [value]: response.rates[value] }
}
console.log(currencyExchangeRates);
res.json({
results: {
"base": base,
"date": response.date,
"rates": currencyExchangeRates
}
})
res.end();
});
});
app.use('/api', exchangeRouter);
app.get('/', (req, res) => {
res.send('Welcome to my nodemon API')
});
app.listen(port, () => {
console.log(`Running on port ${port}`);
})
When attempting to load JSON data from MongoDb while on the deployed version of the website it returns false. There are no issues when doing it locally. Checking heroku logs in the CLI tells me that in the issues function (located in issues.js), it "can't read property toArray() of undefined".
There are three main files: server.js
'use strict';
const express = require('express'),
app = express(),
issues = require('./routes/issues'),
users = require('./routes/users'),
bodyParser = require('body-parser');
app.use(bodyParser.json());
// routing for issue management
app.get('/issues', issues.getAllIssues);
app.put('/issues/issues.json', issues.putNewIssue);
app.post('/issues/:issue_id.json', issues.postIssueUpdate);
app.delete('/issues/:issue_id.json', issues.deleteIssue);
// routing for user management
app.get('/users', users.getAllUsers);
app.put('/users/users.json', users.putNewUser);
app.get('/', (req, res) => {
res.header('Content-Type', 'text/html');
res.end('<h1>Issue Manager</h2>');
})
let port = process.env.PORT;
if (port == null || port =='') {
port = 3000;
}
app.listen(port);
... the function from the routing.js that produces the "false" results
const im = require('issue_manager');
exports.getAllIssues = async (req, res) => {
let allIssues = await im.issues();
console.log(allIssues);
console.log('Operation complete.');
res.json(allIssues);
}
... the module, issues.js, that contains the MongoDb client and processes the request
'use strict';
const MongoClient = require('mongodb').MongoClient;
let db, allIssues;
(async function() {
const uri = 'censored';
const dbName = 'IssueManager';
let client;
client = MongoClient.connect(uri,
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(data => {
return data;
})
.catch(err => {
return err;
});
try {
db = (await client).db(dbName);
allIssues = (await db).collection('Issues').find();
} catch (e) {
console.log(e);
}
})();
exports.issues = async () => {
try {
return await allIssues
.toArray()
.then((data) => {
return data;
})
.catch((err) => {
return err;
});
} catch (e) {
console.error(e);
return false;
}
}
If your code runs fine in your local environment then I think you should:
Confirm that your deployed application can connect to your MongoDB server.
If it can't, verify that you are using the right environment variables / credentials in your deployed application.
This is a common mistake we all experience when deploying. =)
I'm relatively new with mongodb and express and I wish to save data which has been retrieved via an api call, to my database. For some reason my server saves the data twice (creates two documents with same details but different id's) for a single get request and I can't figure out why
const log = console.log;
const express = require('express')
const port = process.env.PORT || 8000
const movieServer = require('./movie-getter')
const { Movie } = require('./model/Movie')
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/ConspireView', { useNewUrlParser: true});
const app = express()
app.get('/movie/:name/:year', (req, res) => {
const name = req.params.name
const year = req.params.year
// let movieObject
movieServer.getMovie(name, year).then((result) => {
new Movie({
name: result.title,
year: result.release_date,
poster: result.poster_path,
banner: result.backdrop_path,
numOfDiscussions: 0,
numOfComments: 0,
vote_average: 0
// discussions: null
}).save().then(result => {
res.send(result)
})
}).catch((error) => {
log(error)
})
})
Are there any syntactic errors here?
This is how I've setup my expressjs server and my mongoDB (mongoDB native driver, not mongoose). Now I would like to check for some existing documents in the DB to add some fixture documents on start of the server.
I do not understand where to do that.
Something like:
const hasAdmin = db.collection('users').findOne({ username: 'admin' })
if (!hasAdmin) {
// Add some data to collection
}
app.js
const app = express()
const mongoDB = mongodb://localhost:27017/mydb
// Parse application/json
app.use(bodyParser.json())
// Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
// GraphQL
app.use('/graphql',
bodyParser.text({ type: 'application/graphql' }),
graphqlMiddleware,
graphqlExpress(req => ({
schema: schema
}))
)
// Connect to Mongo on start
db.connect(mongodb, (err) => {
if (err) {
console.error('Unable to connect to Mongo: ' + err)
process.exit(1)
} else {
app.listen(port, () => {
console.log('Listening on port ' + port)
})
}
})
module.exports = app
You can in theory put it anywhere there is a Mongo connection open.
I would recommend to extract your database-related functionality into a separate class (and a separate file for that; separation of concerns).
You could then call the functionality that fills your database initially from the constructor of that class.
Something like this (this is not working code, but should give you an idea):
db-helper.js
class DbHelper {
constructor() {
this.connect();
}
connect() {
// establish mongo connection
mongo.connect("...", (err, db) => {
this.db = db;
// then take care of initial filling
this.ensureDatabaseIsFilled();
});
}
ensureDatabaseIsFilled() {
const hasAdmin = this.db.collection('users').findOne({ username: 'admin' })
if (!hasAdmin) {
// Add some data to collection
}
}
/**
* Random method that retrieves data from mongo
*/
getRandomThing() {
return new Promise((resolve, reject) => {
this.db.find({...}, (err, result) => resolve(result));
});
}
}
module.exports = DbHelper;
app.js
const DbHelper = require('./db-helper');
const dbHelper = new DbHelper();
// ....
app.get('/example', (req, res) => {
dbHelper.getRandomThing().then(result => res.send(result));
});
// ....