I am new to swagger. I am creating an express-nodejs-typescript, rest api project. I have configured swagger and it is working fine, please see my code below.
import swaggerUi from "swagger-ui-express";
import swaggerJsdoc from 'swagger-jsdoc'
const app = express()
const swaggerOptions: swaggerJsdoc.Options = {
definition: {
openapi: "3.0.0",
info: {
title: "REST API Docs",
version: '1.0',
},
components: {
securitySchemas: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: [
{
bearerAuth: [],
},
],
},
apis: ['src/apis/**/*.controller.ts', 'src/schemas/*.schema.ts'],
};
const swaggerDocs = swaggerJsdoc(swaggerOptions);
app.use(
"/docs",
swaggerUi.serve,
swaggerUi.setup(swaggerDocs, { explorer: true })
);
What i want is to use local swagger.json file, instead on giving apis array apis: ['src/apis/**/*.controller.ts', 'src/schemas/*.schema.ts'],
How can I do that, please help.
something like this should work:
const app = express();
const swaggerUi = require('swagger-ui-express');
try {
const swaggerDoc = require('./your/doc/swagger.json');
app.use('/doc', swaggerUi.serve, swaggerUi.setup(swaggerDoc));
} catch (error) {
console.error('Error loading Swagger json: ', error);
}
app.listen(3000, '0.0.0.0', () => {
console.log(`🚀 Server started: http://localhost:3000/doc`);
});
Related
My project structure
client part contains vue app and server part contains nodejs. In client side handling api service which is created from server. Created api services inside client folder to get response from server.
here is my structure of client-> services->api.js.
here is client -> services -> api.js code:
import axios from 'axios'
export default() => {
return axios.create({
baseURL: `https://dev-cloudthrifty-com.firebaseapp.com`
// https://dev-cloudthrifty-com.firebaseapp.com/
// http://localhost:8081
})
}
client -> vue.config.js configuration file.
const path = require('path')
module.exports = {
devServer: {
compress: true,
disableHostCheck: true,
},
outputDir: path.resolve(__dirname, '../server/dist'), // build all the assets inside server/dist folder
pluginOptions: {
'style-resources-loader': {
preProcessor: 'scss',
patterns: [path.resolve(__dirname, './src/styles/global.scss')]
}
},
chainWebpack: config => {
if (config.plugins.has('optimize-css')) {
config.plugins.delete('optimize-css')
}
}
}
Here is my firebase configuration: firebase.json
{
"hosting": {
"public": "./server/dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{ "source": "**", "function": "app"}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"headers": [ {
"source": "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers": [ {
"key": "Access-Control-Allow-Origin",
"value": "*"
} ]
}, {
"source": "**/*.#(jpg|jpeg|gif|png)",
"headers": [ {
"key": "Cache-Control",
"value": "max-age=7200"
} ]
}, {
"source": "404.html",
"headers": [ {
"key": "Cache-Control",
"value": "max-age=300"
} ]
} ],
"cleanUrls": true
}
}
Server-> app.js where i'm creating api for front-end
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const firebase = require('firebase');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
const credentials = new Buffer('testing:testing123').toString('base64')
const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors())
var Post = require("./models/post");
const firebaseConfig = {
apiKey: "AIzaSyDHDKttdke4WArImpRu1pIU",
authDomain: "dev-cxxxxxy-com.firebaseapp.com",
databaseURL: "https://dev-cxxxx-com.firebaseio.com",
projectId: "dev-clxxxx-com",
storageBucket: "dev-cxxxx-com.appspot.com",
messagingSenderId: "830534343916",
appId: "1:83916:web:e0fd232ebb1"
};
const firebaseApp = admin.initializeApp(firebaseConfig);
var db = firebaseApp.firestore();
app.get('/gcp-scheduler', (req, res) => {
res.send(
[{
title: "Hello World!",
description: "Hi there! How are you?"
}]
)
})
// serve dist folder
if (process.env.NODE_ENV === 'production') {
// Static folder
app.use(express.static(__dirname + '/dist'));
// Handle SPA
app.get('**', (req, res) => res.sendFile(__dirname + '/dist/index.html'));
}
// Add new post
app.post('/list-server', (req, res) => {
var token = req.body.token;
var ccExp = req.body.ccExp;
var cardNumber = req.body.cardNumber;
var currentUserUUID = req.body.currentUserUUID;
var amount = req.body.amount;
console.log(token);
console.log(ccExp);
(async() => {
const paymentRequest = await getAuthTokenForThePayment({
account: cardNumber,
merchid: '496160873888',
amount: amount, // Smallest currency unit. e.g. 100 cents to charge $1.00
expiry: ccExp,
currency: 'USD'
});
const charge = await makeCharge({
merchid: paymentRequest.data.merchid,
retref: paymentRequest.data.retref
});
console.log(charge);
console.log(charge.data.respstat)
if (charge.data.respstat == 'A'){
var data = db.collection('transactionTable').doc(charge.data.retref);
var setAlan = data.set({
'respstat': charge.data.respstat,
'retref': charge.data.retref,
'account': charge.data.account,
'token': charge.data.token,
'batchid': charge.data.batchid,
'amount': charge.data.amount,
'resptext': charge.data.resptext,
'respcode': charge.data.respcode,
'commcard': charge.data.commcard,
'setlstat': charge.data.setlstat,
});
res.send(charge.data.respstat);
} else if(charge.data.respstat == 'B'){
console.log("Declined");
res.send(charge.data.respstat);
}else if(charge.data.respstat == 'C'){
console.log("Retry");
res.send(charge.data.respstat);
}
})();
})
const getAuthTokenForThePayment = async (data) => {
try {
const config = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${credentials}`
}
};
const URL = 'https://fts.cardconnect.com:6443/cardconnect/rest/auth';
return await axios.put(URL, data, config);
} catch (error) {
throw (error);
}
}
const makeCharge = async (data) => {
try {
const config = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${credentials}`
}
};
const URL = 'https://fts.cardconnect.com:6443/cardconnect/rest/capture';
return await axios.put(URL, data, config);
} catch (error) {
throw (error);
}
}
// app.listen(process.env.PORT || 8081)
exports.app = functions.https.onRequest(app);
My issue when i click on checkout button in cart view, it should send current user card details with token to the server(app.js) through post api. After receiving card details from front-end, it should call cardconnect charge api functionality which i have implemented in app.js. In browser console current user card details received successfully but while clicking checkout charge api are not called it shows some other which is unrelated to api.
But everything works in localhost. client side localhost url: http://localhost:8080 and server side localhost url: http://localhost8081.
my firebase hosting url: https://dev-xxxx.firebaseapp.com
here is my console output screenshot:
Dont where i am making mistakes in firebase vuejs/nodejs hosting, rewrites path.
it show error in postservice.js file
import Api from '#/services/Api'
export default {
fetchPosts () {
return Api().get('gcp-scheduler')
},
addPost (params) {
return Api().post('list-server', params)
}
}
Any help much appreciated pls..
How can I authenticate to Feathersjs (https://docs.feathersjs.com/api/client/socketio.html#authentication) using Direct Connection (https://docs.feathersjs.com/api/client/socketio.html#direct-connection)? The following code says that my accessToken is malformed, but I suspect there's more than that to get it working. Where do I fetch an accessToken?
app.js (client):
import express from 'express';
const socket = require('socket.io-client')('http://localhost:3030', {
transports: ['websocket']
});
socket.emit('authenticate', {
strategy: 'jwt',
accessToken: 'what to enter here'
}, (message: any, data: any) => {
console.log(message);
console.log(data);
});
const app = express();
app.get('/', (req, res) => res.send('Up and running!'));
app.listen(4390, () => console.log('Example app listening on port 4390!'));
authentication.js (feathers server)
const authentication = require('#feathersjs/authentication');
const jwt = require('#feathersjs/authentication-jwt');
const local = require('#feathersjs/authentication-local');
module.exports = function (app) {
const config = app.get('authentication');
// Set up authentication with the secret
app.configure(authentication(config));
app.configure(jwt());
app.configure(local());
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies),
],
remove: [
authentication.hooks.authenticate('jwt')
]
}
});
};
I tried using the secret as an accessToken but it didnt work :)
default.json (feathers server config)
"authentication": {
"secret": "r323r32rada86700f18d82ea1d3e74fb58141dbefcd7460ef71736759265e347151a12d68dff50aa59c05e2f18db88661be8ae91a3f12932fddea8891b5ca9f63b5e4fc650edabc59d0d14e8fe4ea54256e7e386845321ab58a320a9ec99438bd058a3fbbda65dadf97bc9585ea82f72201f932beqwdqwdqwd5761a0d0be3e95474db5b9c8a3f4c7303beed0344a1768ba5dad6a1c916d183ea5dd923587768661ff0b08f25ed85dff4ff4e6b58327fe5914e5e7fb2356ee67754b102434f22686444a35fc38c75bcdd6386240a22e0cf62bdc7f227200868da387174b365af2afa7dec378c4ccf22956b134a3ec961fd1ba8d3dc85a7594ab711",
"strategies": [
"jwt",
"local"
],
"path": "/authentication",
"service": "users",
"jwt": {
"header": {
"typ": "access"
},
"audience": "https://yourdomain.com",
"subject": "anonymous",
"issuer": "feathers",
"algorithm": "HS256",
"expiresIn": "1d"
},
"local": {
"entity": "user",
"usernameField": "email",
"passwordField": "password"
}
},
...
Thankful for all replies!
In order to get an accessToken you will typically need to authenticate with a strategy using email/password or oauth. This will return an accessToken which you can then use for jwt authentication.
An alternative approach would be to use a custom authentication strategy which would allow you to have a shared secret that both servers could use to communicate with each other.
Thank you #mchaffe! I managed to solve it with your help. Here is the code used:
import dotenv from 'dotenv';
// Load environments
const config = dotenv.config()
if (config.error) throw config.error
const io = require('socket.io-client');
const feathers = require('#feathersjs/client');
const localStorage = require('localstorage-memory');
const client = feathers();
const socket = io('http://localhost:3030/', {
transports: ['websocket'],
forceNew: true
});
client.configure(feathers.socketio(socket), {
timeout: 10000
});
client.configure(feathers.authentication({
jwtStrategy: 'jwt',
storage: localStorage,
storageKey: 'some-token'
}));
const payload = {
strategy: 'local',
email: process.env.FEATHERS_AUTHENTICATION_EMAIL,
password: process.env.FEATHERS_AUTHENTICATION_PASSWORD
};
client.authenticate(payload).then((response: any) => {
// Do stuff to hooray here
console.log('Access Token: ' + response.accessToken);
// Works!
socket.emit('get', 'logger', 1, (error: any, log: any) => {
console.log('Found log: ' + JSON.stringify(log));
});
}).catch((e: any) => {
console.log('Error: ' + e);
});
I am all ears if you have suggestion on improvements! :) It seems I can access data from the database using the socket.emit method. Do I need to verify the accessToken returned? Thanks again!
I have created a node js project with graph ql (with a very basic schema) but when i am trying to start the server after registering the plugins for graphql and graphiql, i am getting the register is missing error. Below is my code
const hapi=require('hapi');
const { graphqlHapi, graphiqlHapi } = require('apollo-server-hapi');
const { makeExecutableSchema } = require('graphql-tools');
const graphqlSchema = require('./graphql/schema');
const createResolvers = require('./graphql/resolvers');
const executableSchema = makeExecutableSchema({
typeDefs: [graphqlSchema],
resolvers: createResolvers(),
});
const server=hapi.server({
port: 4000,
host:'localhost'
});
server.register({
plugin: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: () => ({
pretty: true,
schema: executableSchema,
}),
},
});
server.register({
plugin: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
},
},
});
const init= async()=>{
routes(server);
await server.start();
console.log(`Server is running at: ${server.info.uri}`);
}
init();
I had initially given the key name as register instead of plugin in the server.register() functions. In either case, i am getting the below error
(node:19104) DeprecationWarning: current URL string parser is
deprecated, and will be removed in a future version. To use the new
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:19104) UnhandledPromiseRejectionWarning: AssertionError
[ERR_ASSERTION]: I nvalid plugin options {
"plugin": {
"options": {
"path": "/graphql",
"graphqlOptions": () => ({\r\n pretty: true,\r\n schema: exe cutableSchema,\r\n })
},
"register" [1]: -- missing -- } }
Please help me out in understanding whenter code herey this happening and how it can be rectified.
Below is the dependencies in my project
apollo-server-hapi": "^2.3.1", "graphql": "^14.0.2", "graphql-tools":
"^4.0.3", "hapi": "^17.8.1",
EDIT
Code after making the suggested changes
const hapi=require('hapi');
const { graphqlHapi, graphiqlHapi } = require('apollo-server-hapi');
const { makeExecutableSchema } = require('graphql-tools');
const graphqlSchema = require('./graphql/schema');
const createResolvers = require('./graphql/resolvers');
const executableSchema = makeExecutableSchema({
typeDefs: [graphqlSchema],
resolvers: createResolvers(),
});
async function start_server() {
const server=hapi.server({
port: 4000,
host:'localhost'
});
await server.register({
plugin: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: () => ({
pretty: true,
schema: executableSchema,
}),
route: {
cors: true,
},
},
});
await server.register({
plugin: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
},
route: {
cors: true,
},
},
});
try {
await server.start();
console.log(`Server is running at: ${server.info.uri}`);
} catch (err) {
console.log(`Error while starting server: ${err.message}`)
}
}
start_server();
There is no need to register the plugins in the latest release of apollo-server-hapi. It contains GraphQL playground instead of graphiql.
The below changes need to be done instead of registering.
const {ApolloServer} = require('apollo-server-hapi');
const executableSchema = makeExecutableSchema({
typeDefs: [graphqlSchema],
resolvers: createResolvers(),
});
const server = new ApolloServer({
schema:executableSchema
});
async function start_server() {
const app=hapi.server({
port: 4000,
host:'localhost'
});
await server.applyMiddleware({ app });
try {
await app.start();
console.log(`Server is running at: ${app.info.uri}`);
} catch (err) {
console.log(`Error while starting server: ${err.message}`)
}
}
start_server();
I want to create rest API with seneca-web (express). I could not find any (full) documentation for a routes file used in it. I base one these examples. Let's assume i have a resource called Task. I want to have these http methods:
GET /tasks
GET /tasks/:taskId
POST /tasks
Here is routes.js:
module.exports = [
{
prefix: '/tasks',
pin: 'role:api,path:*',
map: {
all: {
GET: true,
prefix: ''
},
':taskId': {
GET: true
}
}
},
{
pin: 'role:api,path:*',
map: {
tasks: {
POST: true
}
}
}
]
and my seneca plugin for handling:
module.exports = function task (options) {
this.add({role: 'api', path: 'all'}, function (msg, respond) {
console.log(msg)
this.act('role:task,cmd:all', respond)
respond(null, [{name: 'First Task', description: 'Description of the First Task'}])
})
this.add({role: 'api', path: '*'}, function (msg, respond) {
console.log(msg)
this.act('role:task,cmd:single', {taskId: msg.args.params.taskId}, respond)
})
}
I am not sure how to separate POST and GET actions here.
I found also problematic the fact that keys in map object of routes are taken as a part of a path, eg. GET /tasks/all instead of GET /tasks.
Thanks for any help.
here is example of seneca-web with routes
=========index.js=======
const seneca = require('seneca')()
const express = require('express')()
const web = require('seneca-web')
const cors = require('cors')
var Routes = [{
prefix: '/products',
pin: 'area:product,action:*',
map: {list: {GET: true}}
}]
express.use(cors())
var config = {
routes: Routes,
adapter: require('seneca-web-adapter-express'),
context: express,
options: {parseBody: true}
}
seneca.client()
.use(web, config)
.ready(() => {
var server = seneca.export('web/context')()
server.listen('8082', () => {
console.log('server started on: 8082')
})
})
seneca.add({area: 'product', action: 'list'}, function (args, done) {
try {
done(null, {response: 'Product List'})
} catch (err) {
done(err, null)
}
})
start app using command :
node index.js
open link in your browser
http://localhost:8082/products/list
I am having the following code in my executable Js after the necessary imports.
seneca.ready(function(err){
seneca.act('role:web', {use:{
prefix: '/products',
pin: {area:'product', action:'*'},
map: {
list:{GET:true}
}
}})
var express = require('express');
var app = express();
app.use(require('body-parser').json());
app.use( seneca.export('web') );
app.listen(8082);
});
I am getting the following error while trying to run this example:
Seneca Fatal Error
Message: seneca: The export web has not been defined by a plugin.
Code: export_not_found
Details: { key: 'web' }
Thanks,
sumit
I am a beginner, I hope this snippet will be useful:
var seneca = require('seneca')()
var Web = require("seneca-web");
var Express = require('express');
var app = Express();
var config = {
Routes : [ {
prefix : '/products',
pin : {
area : 'product',
action : '*'
},
map : {
list : {
GET : true
}
}
}
],
adapter : require('seneca-web-adapter-express'),
context : app
};
seneca.use(Web, config);
seneca.add({
role: "web",
area : "product",
action : "list"
}, function(req, done) {
done(null,{result: "my list of products"});
});
seneca.ready(function(err) {
app.use(require('body-parser').json());
app.use(seneca.export('web/context'));
app.listen(8082);
seneca.act('role:web,area:product,action:list',console.log);
});
Seneca web has recently encountered some changes and you should use an adapter for express. You can see examples here on the seneca-web github page
Example : index.js
const seneca = require('seneca')()
const express = require('express')()
const web = require('seneca-web')
const cors = require('cors')
var Routes = [{
prefix: '/products',
pin: 'area:product,action:*',
map: {list: {GET: true}}
}]
express.use(cors())
var config = {
routes: Routes,
adapter: require('seneca-web-adapter-express'),
context: express,
options: {parseBody: true}
}
seneca.client()
.use(web, config)
.ready(() => {
var server = seneca.export('web/context')()
server.listen('8082', () => {
console.log('server started on: 8082')
})
})
seneca.add({area: 'product', action: 'list'}, function (args, done) {
try {
done(null, {response: 'Product List'})
} catch (err) {
done(err, null)
}
})