I have a node.js command line tool that I'd like to run in order to generate an asset in response to an HTTP Request that I receive in Vapor 4. Is it possible to do this?
I guess it is something like this
func requestHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
let process = Process()
// e.g. use `which node` to find path to `node`
process.executableURL = URL(fileURLWithPath: "/path/to/binary") // e.g. /usr/bin/node
// in which folder execute the command, it is optional
process.currentDirectoryPath = "/path/to/folder"
// optional arguments, e.g. if your arguments are -c release then it should be ["-c", "release"]
process.arguments = ["arg1", "arg2", "argN"]
// wait for termination in closure
process.terminationHandler = { process in
switch process.terminationStatus {
// probably normal termination via SIGTERM or when process successfully finished
case 0:
promise.succeed(.ok)
default:
promise.fail(Abort(.failedDependency, reason: "Process finished with code \(process.terminationStatus)"))
}
}
// don't forget to launch it
try process.run()
return promise.futureResult
}