How to pass pusher to utility classes in node.js - javascript

I am using Node.js here with Pusher,
but it's not a pusher related question, it's more of a Node.js question.
In Server.js (declare pusher, and used pusher once for authentication)
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser'
import Pusher from 'pusher'
const PORT = 4001;
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
const pusher = new Pusher({
appId: "ddsd",
key: "afdafa",
secret: "asdfasdfasfadsf",
cluster: "mt1",
useTLS: true
});
app.post("/pusher/user-auth", (req, res) => { //<--pusher is used once here for authentication
const socketId = req.body.socket_id;
const user = {id: '12345'};
const authResponse = pusher.authenticateUser(socketId, user);
res.send(authResponse);
});
I have a utility file call controllers/messagesWebsocketController
class messagesWebsocketController()
async sendMessageToClients (message) {
pusher.sendToUser("12345", "my-event1", { message}); //<--how do I pass pusher in?
}
const messagesWebsocketController = new MessagesWebsocketController();
export default messagesWebsocketController;
So other classes can simply
import messagesWebsocketController from 'controllers/messagesWebsocketController'
...
messagesWebsocketController.sendMessageToClients("hello");
The question is for controllers/messagesWebsocketController, how do I pass the pusher instance in from server.js?

You can create separate file for pusher, export it and you can use it wherever you like in your app.
const pusher = new Pusher({
appId: 'ddsd',
key: 'afdafa',
secret: 'asdfasdfasfadsf',
cluster: 'mt1',
useTLS: true
});
export default pusher;
then use it like this
import pusher from 'pusher.js'

Related

Cannot get sessions in apollo server context?

I ran into that issue:
I cannot get the access the session object in the request object inside the apollo server context, so I get undefined as output.
Otherwise I can access the session object in the request object inside express routes as normal.
How can I solve that?
import express, { Application, Request, Response } from 'express';
import bodyParser from "body-parser";
import helmet from "helmet";
import xss from 'xss';
import moment from "moment";
import passport from "passport";
import { connect } from "mongoose";
import "reflect-metadata";
import { ApolloServer } from 'apollo-server-express';
import { buildSchema, ResolverData } from "type-graphql";
// Import resolvers
import main_config from './main.config';
import AuthenticationRoutes from './Authentication/Authentication.routes';
import './Authentication/Authentication.strategies';
import {
topicResolver, docsResolver, courseResolver, articleResolver, projectIdeaResolver
} from './Graphql/Topics/Topics.resolvers';
import cookieSession from 'cookie-session';
import expressSession from 'express-session';
// const expGql = require("express-graphql");
const app : Application = express();
// Init cookie cookie-session
app.use(cookieSession({
keys : ["IDFVBHNIOVFFBUE"],
name : 'DBDIUN',
secret : "IDFVBHNIOVFFBUE"
}));
async function runapp (){
// Ïnit passport app and routes
app.use(passport.initialize());
app.use(passport.session())
// Run apollo server
const apollo = new ApolloServer({
schema : await buildSchema({
resolvers : [
topicResolver, docsResolver, courseResolver, articleResolver, projectIdeaResolver
],
globalMiddlewares: [],
}),
context: ({ req, res }) =>{
console.log("context")
console.log(req.session.passport) // cannot get session object then Get undefined
return {
getUser: () => req.user,
logout: () => req.logout(),
}
},
playground : true
})
apollo.applyMiddleware({ app });
// Init body parser and helmet
app.use(helmet());
app.use(bodyParser.json());
app.use('/auth', AuthenticationRoutes)
}
runapp();
Don´t deconstruct req work for me:
context: (req, res) => {
return {
req: req,
res: res,
}
};

NextJS throwing 404 when deployed to Vercel

I have a custom express server that I'm using with NextJS.
Everything works just fine when I'm developing locally, but when I deploy to Vercel, I catch 404s whenever I try to access my backend API.
What could be going wrong? Here's my server.ts:
import express from 'express';
import next from 'next';
import bodyParser from 'body-parser';
import { connectDbs } from './config/db';
import { listingsRouter } from './routes';
const PORT = process.env.PORT || 3003;
const dbs = ['mydb'];
const dev = process.env.NODE_DEV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();
const applyMiddleware = (app) => {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
};
const applyRoutes = (app) => {
app.use('/api/listings', listingsRouter);
};
const startServer = async () => {
await nextApp.prepare();
const app = express();
applyMiddleware(app);
applyRoutes(app);
app.get('*', (req, res) => handle(req, res));
await connectDbs(dbs);
app.listen(PORT, () => console.log(`App listening on port ${PORT}`));
};
startServer();
The Next.js documentation for custom servers:
A custom server cannot be deployed on Vercel, the platform Next.js was made for.
Source: Custom Server
Even though you can't deploy a custom server, you generally don't need to do so because Next.js supports Serverless Functions using the /pages/api directory.
Source: API Routes
Also take a look at this Guide explaining how to convert custom Next.js server to routes:
Source: Server to Routes Guide

Cant make a fetch post request from front end client to local node server, only GET requests work

When making a post request to my local server (localhost:8080/page) i am shown 'Cannot GET /page'. I am enter image description hereusing Google Chrome where the dev tools network tab shows:
Here is my front end code with the simple fetch statement:
import React, { Component } from 'react'
import io from 'socket.io-client'
import OAuth from './OAuth'
import Loading from './Loading'
import Footer from './Footer'
import Tweets from './Tweets'
import { API_URL } from './config'
import './App.css'
const socket = io(API_URL) //,{transports: ['websocket']}
const providers = ['twitter', 'google', 'facebook', 'github']//import theese in because our auth controller needs to call our event for each provider which the event is named after the provider
export default class App extends Component {
state = {
loading: true
}
componentDidMount() {
socket.on('connect', function(){
console.log('connected')
});
fetch(`${API_URL}/wake-up`)
.then(res => {
if (res.ok) {
this.setState({loading: false})
}
})
const data = {
random1: 'random',
random2: 'random-input'
}
const options = {
method:'POST',
mode: 'cors',
header: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}
fetch(`${API_URL}/page`, options);
}
render() {
const divStyles = {
}
const buttons = (providers, socket) =>
providers.map(provider =>
<OAuth
provider={provider}
key={provider}
socket={socket}
/>
)
return (
<div className='wrapper'>
<div className='container'>
{this.state.loading
? <Loading />
: buttons(providers, socket)
}
</div>
{
buttons
? <Tweets provider='twitter' />
: <div> Hasnt fetched</div>
}
<Footer />
</div>
)
}
}
Here is my backend node server (server.js) where i have app.post available but still no results and ive attached my routes file below that:
require('dotenv').config()
const express = require('express')
const path = require('path')
const fs = require('fs')
const https = require('https')
const http = require('http')
const passport = require('passport')
const session = require('express-session')
const cors = require('cors')
const socketio = require('socket.io')
const authRouter = require('./lib/auth.router')
const passportInit = require('./lib/passport.init')
const { SESSION_SECRET, CLIENT_ORIGIN } = require('./config')
const bodyParser = require('body-parser')
const app = express()
let server
// If we are in production we are already running in https
if (process.env.NODE_ENV === 'production') {
server = http.createServer(app)
}
// We are not in production so load up our certificates to be able to
// run the server in https mode locally
else {
const certOptions = {
key: fs.readFileSync(path.resolve('./certs/server.key')),
cert: fs.readFileSync(path.resolve('./certs/server.crt'))
}
server = https.createServer(certOptions, app)
}
// Setup for passport and to accept JSON objects
app.use(express.json())
app.use(bodyParser.json());
app.use(passport.initialize())
passportInit()
// Accept requests from our client
app.use(cors())
// saveUninitialized: true allows us to attach the socket id to the session
// before we have athenticated the user
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}))
// Connecting sockets to the server and adding them to the request
// so that we can access them later in the controller
const io = socketio(server)
app.set('io', io)
// Catch a start up request so that a sleepy Heroku instance can
// be responsive as soon as possible
app.get('/wake-up', (req, res) => res.send('👍'))
app.post('/page', (req, res)=>{
res.setHeader('Content-Type', 'application/json');
res.send('yes');
})
// Direct other requests to the auth router
app.use('/', authRouter)
server.listen(process.env.PORT || 8080, () => { //calllback function
console.log('listening...')
})
Route file auth.router.js:
const express = require('express')
const router = express.Router()
const passport = require('passport')
const authController = require('./auth.controller')
// Setting up the passport middleware for each of the OAuth providers
const twitterAuth = passport.authenticate('twitter')
const googleAuth = passport.authenticate('google', { scope: ['profile'] })
const facebookAuth = passport.authenticate('facebook')
const githubAuth = passport.authenticate('github')
// Routes that are triggered by the callbacks from each OAuth provider once
// the user has authenticated successfully
router.get('/twitter/callback', twitterAuth, authController.twitter)//on callback we are authenticating twitter and then sending our data/user info to the io to be called up on client via the socket.io (socket.on())
router.get('/google/callback', googleAuth, authController.google)
router.get('/facebook/callback', facebookAuth, authController.facebook)
router.get('/github/callback', githubAuth, authController.github)
// This custom middleware allows us to attach the socket id to the session
// With that socket id we can send back the right user info to the right
// socket
router.use((req, res, next) => {
req.session.socketId = req.query.socketId
next()
})
// Routes that are triggered on the client ////so may not need certain code from the twitter developer page
router.get('/twitter', twitterAuth)
router.get('/google', googleAuth)
router.get('/facebook', facebookAuth)
router.get('/github', githubAuth)
module.exports = router
You need to define your route for both get and post if you want to be able to reach it with both verbs. You could do something like this:
const handler = (req, res)=> {
res.setHeader('Content-Type', 'application/json');
res.send('yes');
};
app.get('/page', handler);
app.post('/page', handler);

nodejs with Graphql

I am facing an issue, while trying graphQL. I guess I am doing something wrong at schema or resolver. Everything according to docs but still not able to get the data.
What Do I need is very simple user model to run its queries in graphiql.
Here is my code: Download code (zip file) (code for node-7.5)
and then just run npm install and npm run build.
Main files: server.js, schema.js, models/user.js and resolvers/user.js.
models/user.js
const typeDefinitions = `
type User {
id: String!
name: String
type: Int
isActive: Boolean
clients: [String]
}
schema {
query: User
}
`;
export default [typeDefinitions]
resolvers/user.js:-
const resolverMap = {
User: {},
};
export default resolverMap
schema.js
import { makeExecutableSchema } from 'graphql-tools';
import resolver from '../resolvers/user'
import User from './user';
export default makeExecutableSchema({
typeDefs: User,
resolvers: resolver,
});
server.js:-
import express from 'express';
import bodyParser from 'body-parser';
import routes from './app/routers/index';
import { graphqlExpress } from 'graphql-server-express';
import schema from './app/models/schema'
var app = express(); // create our app w/ express
var database = require('./app/configs/database');
var port = process.env.PORT || 8888; // set the port
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use('/graphql', bodyParser.json(), graphqlExpress({
graphiql: true,
pretty: true,
schema: schema
}));
app.listen(port);
console.log("App listening on port : " + port);
import { graphiqlExpress } from 'graphql-server-express';
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
You need to use above code,
graphiql: true is not supported anymore I guess,
You need to import a graphiql method and pass an endpoint for the same

ExpressJs - route params are not showing in the req.params property

New to nodejs world. Trying to create a REST Service with express and I'm stuck trying to debug this issue... I want to call a GET resource that has a route param in router.use('/applications/:appId/messages', messageRoutes);
index.js
import Promise from 'bluebird';
import mongoose from 'mongoose';
import config from './config/env';
import app from './config/express';
// plugin bluebird promise in mongoose
mongoose.Promise = Promise;
// connect to mongo db
mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } });
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${config.db}`);
});
const debug = require('debug')('express-mongoose-es6-rest-api:index');
// listen on port config.port
app.listen(config.port, () => {
debug(`server started on port ${config.port} (${config.env})`);
});
export default app;
/config/express.js
import express from 'express';
import bodyParser from 'body-parser';
import routes from '../server/routes';
const app = express();
// parse body params and attache them to req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
[...]
// mount all routes on /api path
app.use('/api', routes);
[...]
export default app;
/server/routes/index.js
import express from 'express';
import userRoutes from './user';
import messageRoutes from './message';
import applicationRoutes from './application';
import authRoutes from './auth';
const router = express.Router(); // eslint-disable-line new-cap
// mount message routes at /messages
router.use('/applications/:appId/messages', messageRoutes);
export default router;
/server/routes/message.js
import express from 'express';
import validate from 'express-validation';
import paramValidation from '../../config/param-validation';
import messageCtrl from '../controllers/message';
const router = express.Router(); // eslint-disable-line new-cap
router.route('/')
/** GET /api/applications/:appId/messages - Get list of messages */
.get(messageCtrl.getByAppId);
export default router;
/server/controllers/message.js
import mg from 'mongoose'
import Message from '../models/message';
import moment from 'moment';
function getByAppId(req, res, next) {
//return res.json(req.message);
console.log(req);
}
export default {getByAppId};
The result of console.log(req) is a printout of the request object but the params array is blank.
baseUrl: '/api/applications/57fcf8129eb1d52f1cb10332/messages',
originalUrl: '/api/applications/57fcf8129eb1d52f1cb10332/messages/',
_parsedUrl:
Url {
protocol: null,
slashes: null,
[...]
params: {},
query: {},
Can anyone offer any guidance? Much appreciated. Not sure why the params are blank. Could it be the :appId variable declaration in express router.use() statement?

Categories