【问题标题】:How to avoid adding localhost to .js src如何避免将 localhost 添加到 .js src
【发布时间】:2019-02-10 22:54:25
【问题描述】:

我完全被困在跟随中。一旦我使用node app.js 启动我的应用程序并在node watch 之后进入控制台:Загрузка <script> по адресу «http://localhost:8000/sheetrock.js» не удалась. 这意味着从本地主机加载脚本没有管理。但是在我的 .html 中我说

<script type="text/javascript" src="./sheetrock.js"></script>

所以 src 中没有 localhost。我想要的是 src 属性不是相对的,而是静态的(没有添加本地主机)。我的代码有什么问题? App.js如下:

const path = require('path');

    const express = require('express');
    const ejs = require('ejs');
    const paypal = require('paypal-rest-sdk');



    const exphbs  = require('express-handlebars');
    const app = express();

    app.engine('handlebars', exphbs({defaultLayout: 'main'}));
    app.set('view engine', 'handlebars');
    // app.set('views', path.join(__dirname, 'views'));
    app.get('/', (req, res) => res.render('index'));


    app.get('/success', (req, res) => {
      const payerId = req.query.PayerID;
      const paymentId = req.query.paymentId;

      const execute_payment_json = {
        "payer_id": payerId,
        "transactions": [{
            "amount": {
                "currency": "USD",
                "total": "25.00"
            }
        }]
      };

      paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
        if (error) {
            console.log(error.response);
            throw error;
        } else {
            console.log(JSON.stringify(payment));
            res.send('Success');
        }
    });
    });

    app.get('/cancel', (req, res) => res.send('Cancelled'));

    app.listen(8000, () => console.log('Server Started'));

【问题讨论】:

  • app.use(express.static(path.join(__dirname, 'yourfolder'))) 将其添加到文件顶部 const app = express(); 之后
  • @PerDigesen 它没有解决问题

标签: javascript node.js localhost


【解决方案1】:

您应该尝试像这样为index.html 提供服务


app.get('/', (req,res) => {
  res.sendFile(path.join(__dirname + '/index.html'))
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    相关资源
    最近更新 更多