Node JS Connect file server not serving as expected

醉酒当歌 提交于 2019-12-25 07:39:13

问题


I'm playing with a simple Connect file server:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static('.'))
    .listen(3000);

The file index.html loads when I visit localhost:3000. But I can't seem to access any other file in the way that I would expect. For example, the address localhost:3000/json-parser.html returns Error: Forbidden followed by information about the Connect module (I won't include it all here unless requested, because it's quite long and I suspect there is a simple answer to this).

I have changed my server, following code here, to serve a 'public' folder within my directory:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static('public'))
    .listen(3000);

But I want access to scripts and files within folders within the parent directory which isn't possible without putting everything in 'public' and having my Connect file server outside that. Is there a way for Connect to serve the directory around it, given that the above doesn't seem to work?


回答1:


Try:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static(__dirname))
    .listen(3000);

However bear in mind that this will serve ALL the files and subdirectories underneath the directory where you ran server.js which is generally NOT a good plan.



来源:https://stackoverflow.com/questions/15309531/node-js-connect-file-server-not-serving-as-expected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!