setting current directory of node.exe to the location of the running *.js file

This commit is contained in:
Tomasz Janczuk 2011-09-26 15:02:33 -07:00
Родитель e23c26fe5e
Коммит 5861da6d38
7 изменённых файлов: 69 добавлений и 2 удалений

Просмотреть файл

@ -55,6 +55,10 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
DWORD environmentSize;
DWORD flags;
HANDLE job;
PWSTR currentDirectory = NULL;
PSTR currentDirectoryA = NULL;
DWORD currentDirectorySize = 0;
DWORD currentDirectorySizeA = 0;
RtlZeroMemory(&processInformation, sizeof processInformation);
RtlZeroMemory(&startupInfo, sizeof startupInfo);
@ -91,7 +95,6 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
// create the environment block for the node.js process - pass in the named pipe name;
// this is a zero terminated list of zero terminated strings of the form <var>=<value>
ErrorIf(NULL == (currentEnvironment = GetEnvironmentStrings()), GetLastError());
environmentSize = 0;
do {
@ -104,6 +107,16 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
FreeEnvironmentStrings(currentEnvironment);
currentEnvironment = NULL;
// establish the current directory for node.exe process to be the same as the location of the *.js file
currentDirectory = (PWSTR)context->GetPhysicalPath(&currentDirectorySize);
while (currentDirectorySize && currentDirectory[currentDirectorySize] != L'\\' && currentDirectory[currentDirectorySize] != L'/')
currentDirectorySize--;
ErrorIf(0 == (currentDirectorySizeA = WideCharToMultiByte(CP_ACP, 0, currentDirectory, currentDirectorySize, NULL, 0, NULL, NULL)), E_FAIL);
ErrorIf(NULL == (currentDirectoryA = new char[currentDirectorySize + 1]), ERROR_NOT_ENOUGH_MEMORY);
ErrorIf(currentDirectorySizeA != WideCharToMultiByte(CP_ACP, 0, currentDirectory, currentDirectorySize, currentDirectoryA, currentDirectorySizeA, NULL, NULL), E_FAIL);
currentDirectoryA[currentDirectorySizeA] = '\0';
// create startup info for the node.js process
RtlZeroMemory(&this->startupInfo, sizeof this->startupInfo);
@ -137,7 +150,7 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
TRUE,
flags,
newEnvironment,
NULL,
currentDirectoryA,
&this->startupInfo,
&processInformation
), GetLastError());
@ -189,6 +202,8 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
// clean up
delete [] currentDirectoryA;
currentDirectoryA = NULL;
delete [] newEnvironment;
newEnvironment = NULL;
delete [] fullCommandLine;
@ -199,6 +214,12 @@ HRESULT CNodeProcess::Initialize(IHttpContext* context)
return S_OK;
Error:
if (currentDirectoryA)
{
delete [] currentDirectoryA;
currentDirectoryA = NULL;
}
if (suuid != NULL)
{
RpcStringFree(&suuid);

Просмотреть файл

@ -229,6 +229,7 @@ copy /y $(SolutionDir)\..\config\* $(SolutionDir)\..\..\build\$(Configuration)\$
<None Include="..\..\test\functional\tests\104_echo.js" />
<None Include="..\..\test\functional\tests\105_logging.js" />
<None Include="..\..\test\functional\tests\106_autoupdate.bat" />
<None Include="..\..\test\functional\tests\107_filesystem.js" />
<None Include="..\..\test\functional\tests\200_samples.bat" />
<None Include="..\..\test\functional\tests\node_modules\iisnodeassert.js" />
<None Include="..\..\test\functional\tests\parts\106_autoupdate_first.js" />
@ -249,6 +250,9 @@ copy /y $(SolutionDir)\..\config\* $(SolutionDir)\..\..\build\$(Configuration)\$
<None Include="..\..\test\functional\www\106_autoupdate\hello_first.js" />
<None Include="..\..\test\functional\www\106_autoupdate\hello_second.js" />
<None Include="..\..\test\functional\www\106_autoupdate\web.config" />
<None Include="..\..\test\functional\www\107_filesystem\file.txt" />
<None Include="..\..\test\functional\www\107_filesystem\hello.js" />
<None Include="..\..\test\functional\www\107_filesystem\web.config" />
<None Include="..\config\iisnode_schema.xml" />
<None Include="..\config\iisnode_schema_x64.xml" />
<None Include="..\samples\configuration\hello.js" />

Просмотреть файл

@ -82,6 +82,9 @@
<Filter Include="Tests\functional\scripts">
<UniqueIdentifier>{f69631ee-6910-4fa3-9e6f-36786970c51e}</UniqueIdentifier>
</Filter>
<Filter Include="Tests\functional\www\107_filesystem">
<UniqueIdentifier>{cc6d4108-5cc9-4576-adf4-6ddc7d186998}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
@ -346,5 +349,17 @@
<None Include="..\config\iisnode_schema_x64.xml">
<Filter>Config</Filter>
</None>
<None Include="..\..\test\functional\tests\107_filesystem.js">
<Filter>Tests\functional\tests</Filter>
</None>
<None Include="..\..\test\functional\www\107_filesystem\file.txt">
<Filter>Tests\functional\www\107_filesystem</Filter>
</None>
<None Include="..\..\test\functional\www\107_filesystem\hello.js">
<Filter>Tests\functional\www\107_filesystem</Filter>
</None>
<None Include="..\..\test\functional\www\107_filesystem\web.config">
<Filter>Tests\functional\www\107_filesystem</Filter>
</None>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,9 @@
/*
Current directory of node.exe process is set to the same location as the applications's *.js file.
*/
var iisnodeassert = require("iisnodeassert");
iisnodeassert.sequence([
iisnodeassert.get(10000, "/107_filesystem/hello.js", 200, "Contents of a file")
]);

Просмотреть файл

@ -0,0 +1 @@
Contents of a file

Просмотреть файл

@ -0,0 +1,10 @@
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('file.txt', function (e, d) {
if (e) throw e;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(d);
});
}).listen(process.env.PORT);

Просмотреть файл

@ -0,0 +1,7 @@
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</configuration>