首页 > 解决方案 > 如何避免将 localhost 添加到 .js src

问题描述

我完全陷入了追随。一旦我启动我的应用程序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'));

标签: javascriptnode.jslocalhost

解决方案


你应该尝试提供index.html这样的服务


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


推荐阅读