Ниже приведен проект Node JS < /p>
api
swagger.yaml - having visual representation of the API and its documentation
controllers
entityApi.js file - APIs - create,delete,list,patch api
service
entityService.js - API implementation
utils
mongoUtils.js
index.js file
.... and other files
< /code>
ниже приведен код в файле index.js \ n < /p>
'use strict';
const fs = require('fs'),
path = require('path'),
http = require('http'),
mongoUtils = require('./utils/mongoUtils'),
swaggerUtils = require('./utils/swaggerUtils');
const {error, errorEnum, sendError} = require('./utils/errorUtils');
const app = require('connect')();
const swaggerTools = require('swagger-tools');
const serverPort = 8080;
//error is coming here
fs.copyFileSync(path.join(__dirname, './index.html_replacement'),
path.join(__dirname, './node_modules/swagger-ui-dist/index.html'), (err) => {
if(err) {
console.log('Unable to replace swagger-ui-dist/index.html file - something wrong with the installation ??');
process.exit(1);
}
})
// swaggerRouter configuration
const options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
const swaggerDoc = swaggerUtils.getSwaggerDoc();
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator({
validateResponse: false
}));
// Error handling for validation
app.use(errorHandler);
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
// using the more up-to-date swagger-ui-dist - not the default app.use(middleware.swaggerUi())
app.use(middleware.swaggerUi({ swaggerUiDir: path.join(__dirname, 'node_modules', 'swagger-ui-dist') }));
// Start the server
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
});
// handles timed out requests
function haltOnTimedout(req, res, next) {
if (!req.timedout) {
next();
} else {
debug("\nrequest timed out!\n");
next("the request timed out", null, null, null, 504);
}
}
function errorHandler (err, req, res, next) {
if(err) {
if(err.failedValidation) {
const message = err.results.errors.map(item => item.message).join(", ");
const error = new Error(ErrorEnum.INVALID_BODY, message);
sendError(res,error);
} else {
const error = new Error(ErrorEnum.INVALID_BODY, "Invalid request");
sendError(res,error);
}
} else {
next(err,req,res);
}
};
< /code>
и mongoutils.js Содержание ниже \ n < /p>
'use strict';
const util = require('util')
const assert = require('assert');
const queryToMongo = require('query-to-mongo');
const querystring = require('querystring');
const MongoClient = require('mongodb').MongoClient;
const {getResponseType, getPayloadType, getTypeDefinition} = require('./swaggerUtils');
var mongodb = null;
function connectHelper(callback) {
var config = require('../config.json');
var argv = require('minimist')(process.argv);
var dbhost = argv.dbhost ? argv.dbhost: config.db_host;
const mongourl = process.env.MONGO_URL || (config.db_prot + "://" + dbhost + ":" + config.db_port + "/" + config.db_name);
MongoClient.connect(mongourl, { useNewUrlParser: true }, function (err, db) {
if (err) {
mongodb = null;
callback(err,null);
} else {
mongodb = db.db("mydatabase");
callback(null,mongodb);
}
}
);
};
function getMongoQuery(req) {
var res;
if(req instanceof Object) {
res = queryToMongo(req._parsedUrl.query);
} else {
res = queryToMongo(querystring.parse(req));
}
if(res!==undefined && res.options!==undefined && res.options.fields!==undefined) {
res.options.fields.href = true;
res.options.fields.id = true;
}
try {
const requestType = getPayloadType(req);
const properties = Object.keys(res.criteria);
var typeDefinition = getTypeDefinition(requestType);
if(typeDefinition.properties!==undefined) {
typeDefinition = typeDefinition.properties;
}
properties.forEach(prop => {
var paramDef = typeDefinition[prop];
if(paramDef!==undefined && paramDef.type === "string" && paramDef.format === "date-time") {
const propVal = res.criteria[prop];
// equality test if not the value is an object
if(!(propVal instanceof Object)) {
if(!isNaN(Date.parse(propVal))) {
res.criteria[prop] = {$regex: '^' + propVal + '.*' };
}
}
}
});
}
catch(err) {
// ignore for now
}
res.options.projection = res.options.fields;
delete res.options.fields;
return(res);
};
function quotedString(s) {
return s;
};
function connectDb(callback) {
if(mongodb) {
mongodb.stats(function(err, stats) {
if(stats != null) {
callback(null,mongodb);
} else {
connectHelper(callback);
}
});
} else {
connectHelper(callback);
}
};
function connect() {
return new Promise(function(resolve,reject) {
connectDb(function(err,db) {
if(err!=null || db==null) {
reject(err);
} else {
resolve(db);
};
});
});
};
function sendDoc(res,code,doc) {
// delete internal mongo _id from all documents
if(Array.isArray(doc)) {
// remove _id from all documents
doc.forEach(x => {
delete x._id;
});
} else {
delete doc._id;
}
if(doc.href) {
res.setHeader('Location', doc.href);
}
res.statusCode = code;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(doc));
}
module.exports = { connect, connectDb, getMongoQuery, sendDoc };
< /code>
Установка NPM - это нормально
, где as index.js дает ниже ошибку
binding.copyfile(
** ^
typeerror: режим должен быть int32 или null /undecined
at object.copyfilesync (node: fs: 3086: 11). (C: \ users \ shivashankar.g02 \> \ index.js: 18: 4) в модуле._compile (узел: внутренние/модули/cjs/загрузчик: 1562: 14)
**
at obj (Узел: внутренний/модули/CJS/Loader: 1313: 32)
at function._load (Узел: внутренний/модули/CJS/Loader: 1123: 12)
At TracingChannel.tracesync (Node: inerostics_channel: 322: 14)
at wroudmodload (node: inerostics_channel: 322: 14)
at wroudmodulal (node: modostics/cjs/cjs/cjs)
at function.executeUserentryPoint [as runMain] (Узел: Internal/Modules/Run_Main: 170: 5)
at node: internal/main/run_main_module: 36: 49 {
код: 'err_invalid_arg_type'
power on iTesection Любезно помогать в решении проблемы. Спасибо
Подробнее здесь: https://stackoverflow.com/questions/795 ... connecting
TypeError: режим должен быть int32 или null/undefined в проблеме узла JS при подключении к MongoDB ⇐ Javascript
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Uncaught typeerror: не удается прочитать «параметры" effice 'опции undefined javascript
Anonymous » » в форуме Jquery - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Uncaught typeerror: не удается прочитать «параметры" effice 'опции undefined javascript
Anonymous » » в форуме Jquery - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-