https://github.com/Alesha24/restaurants/blob/master/restplatzproject.zip
上面的链接中有一个小节点项目。可以使用node app.js
命令开始,然后再打开http://localhost:8000/。
但是,它不会在views文件夹中呈现index.handelbars文件。样式和.js文件均未上传。为此,我添加了
app.use(express.static(path.join(__dirname, '/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.use(express.static(path.join(__dirname, '/js')));
// app.use(express.static('js'));
app.engine('handlebars', exphbs({defaultLayhhout: 'main'}));
app.set('view engine', 'handlebars');
// app.set('views', path.join(__dirname, 'views'));
app.get('/', (req, res) => res.render('index'));
app.post('/pay', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:8000/success",
"cancel_url": "http://localhost:8000/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Red Sox Hat",
"sku": "001",
"price": "25.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "25.00"
},
"description": "Hat for the best team ever"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
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'));