以前有效的东西不再有效,我不知道如何解决这个问题。我在 Mac 上运行并具有以下版本: Node: v10.14.1 npm: 6.9.0 express: ^4.16.4 @angular/cli: ^7.3.3 babel-preset-env: ^1.7.0
即使我同时安装了 Express 和 babel-preset-env,我也会收到以下错误消息:
import express from 'express';
^^^^^^^
SyntaxError: Unexpected identifier
这就是我在 server.js 文件中的内容:
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import logger from 'morgan';
import mongoose from 'mongoose';
import SourceMapSupport from 'source-map-support';
import cors from 'cors';
// import routes
import weddingRoutes from './routes/wedding.server.route';
// define our app using express
const app = express();
// allow-cors
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
// configure app
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// var cors = require('cors');
app.use(cors());
// set the port
const port = process.env.PORT || 3001;
// connect to database
var mongoDB = 'mongodb://127.0.0.1/wedding-website-app';
mongoose.connect(mongoDB, {
useNewUrlParser: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// add Source Map Support
SourceMapSupport.install();
app.use('/api', weddingRoutes);
app.get('/', (req,res) => {
return res.send('Api working');
})
// catch 404
app.use((req, res, next) => {
res.status(404).send('<h2 align=center>Page Not Found!</h2>');
});
// start the server
app.listen(port,() => {
console.log(`App Server Listening at ${port}`);
});