我正在尝试使用 npm文件系统列出服务器上给定路径的文件和文件夹。据我了解,浏览器(客户端)不允许使用 Javascript 访问文件系统,但如果我从服务器运行 javascript,则应该允许访问文件系统。
因此,我创建了一个 ReactJS 应用程序,按照此处的教程执行服务器端渲染(或者您可以获取 git 代码并在此处开箱即用地构建/运行它)。
我可以在禁用浏览器的 javascript 的情况下调用 Date.now() 等基本函数,证明 javascript 正在服务器上运行,但是当我插入以下代码时,我收到错误。
为什么我的服务器代码无法识别 fs?
组件代码:
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as userActions from '../actions/user'
import { Link } from 'react-router-dom'
import './FirstPage.css'
var fileSystem = require("file-system")
class FirstPage extends Component {
render() {
fileSystem.recurse('./', ['*.*'],
function(filepath, relative, filename) {
if (filename) {
console.log("FILE"+filename); …Run Code Online (Sandbox Code Playgroud) reactjs ×1