Javascript和模板是并行文件夹,我如何在快递中提供?

时间:2016-02-11 05:12:04

标签: javascript express

问题:

如何更改server.js文件以允许index.html中的three.js脚本错误Cannot GET /node_modules/three/three.js

标记:index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>heyyy friends</h1>
    <script src="../node_modules/three/three.js"></script>
</body>
</html>

JS:server.js

var express = require('express');
var app = express();



app.use(express.static(__dirname + '/../client/'));

app.get('/', (req, res) => {
  res.render('index');
});

app.listen(3000, () => {
  console.log('VTEC JUST KICKED IN Y0');
});

文件夹结构:

Folder

的package.json:

{
  "name": "Morningharwood",
  "version": "1.0.0",
  "description": "",
  "main": "server/server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server/server.js",
    "dev": "nodemon server/server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/matthewharwood/MorningHarwood.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/matthewharwood/MorningHarwood/issues"
  },
  "homepage": "https://github.com/matthewharwood/MorningHarwood#readme",
  "dependencies": {
    "express": "^4.13.4",
    "jade": "^1.11.0",
    "three": "^0.73.2"
  },
  "devDependencies": {
    "nodemon": "^1.8.1"
  }
}

1 个答案:

答案 0 :(得分:2)

server.js

在定义静态内容的当前行下添加以下行:

app.use('/node_modules', express.static(__dirname + '/../node_modules'));


的index.html

更改

<script src="../node_modules/three/three.js"></script>

<script src="/node_modules/three/three.js"></script>