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
Below is the code in index.js file
'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);
}
};
和mongoutils.js的内容
'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 };
NPM安装很好 在哪里,如节点index.js给出以下错误 binding.copyfile( ** ^ TypeError:模式必须为INT32或NULL/未定义 在object.copyfilesync(节点:FS:3086:11) 在对象。 (c:\ users \ shivashankar.g02 \> \ index.js:18:4)在模块上 ** 在object..js(节点:内部/模块/cjs/loader:1699:10) 在Module.load(节点:内部/模块/CJS/LOADER:1313:32) at function._load(节点:内部/模块/CJS/LOADER:1123:12) 在tracingChannel.traceync(节点:Diagnostics_Channel:322:14) 在WrapModuleLoad(节点:内部/模块/CJS/LOADER:217:24) 在function.executeUserenTryPoint [as runmain](节点:内部/模块/run_main:170:5) 在节点:内部/main/run_main_module:36:49 { 代码:'err_invalid_arg_type'
任何关于它要来的指示/指示。请协助解决这个问题。谢谢
copyFileSync
mode
整数面膜。您的第三个参数是回调,会导致此错误。
由于您通过回调,似乎您打算使用copyFile
,而不是copyFileSync
::
fs.copyFile(
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);
}
}
)
对于记录,copyFile
也可以采用一个论点,但是就像使用mode
一样,可以省略此论点。